
<!DOCTYPE HTML>
<html lang="zh-hans" >
<head>
    <meta charset="UTF-8">
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
    <title>3.1：Widget简介 · 《Flutter实战》</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="baidu-site-verification" content="Y34sAqDKUH"/>
    <meta name="theme-color" content="#ffffff">
    <!--<meta name="description" content="">-->
    <meta name="description" content="《Flutter实战》是Flutter中文网（https://flutterchina.club）开源的电子书，旨在帮助开发者系统化的了解并学习Flutter。">
    <meta name="keywords" content="flutter实战,flutter书籍,flutter电子书,flutter中文网 ">
    <meta name="generator" content="GitBook 3.2.3">
    
    <meta name="author" content="wendux 824783146@qq.com">
    
    
    
    
    <link rel="stylesheet" href="https://pcdn.flutterchina.club/gitbook/style.css">

    
    
    
    <link rel="stylesheet" href="https://pcdn.flutterchina.club/gitbook/gitbook-plugin-search-pro/search.css">
    
    
    
    <link rel="stylesheet" href="https://pcdn.flutterchina.club/gitbook/gitbook-plugin-ace/ace.css">
    
    
    
    <link rel="stylesheet" href="https://pcdn.flutterchina.club/gitbook/gitbook-plugin-splitter/splitter.css">
    
    
    
    <link rel="stylesheet" href="https://pcdn.flutterchina.club/gitbook/gitbook-plugin-prism/prism.css">
    
    
    
    <link rel="stylesheet" href="https://pcdn.flutterchina.club/gitbook/gitbook-plugin-donate/plugin.css">
    
    
    
    <link rel="stylesheet" href="https://pcdn.flutterchina.club/gitbook/gitbook-plugin-fontsettings/website.css">
    
    
    

    

    
        
    
        
    
        
    
        
    
        
    

    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="https://pcdn.flutterchina.club/gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="https://pcdn.flutterchina.club/gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="state_manage.html" />
    
    
    <link rel="prev" href="./" />
    

    <style>
        .pull-right > i {
            font-size: 25px;
            color: #777 !important;
            cursor: pointer;

        }

        .pull-right > i:hover {
            color: #777 !important;
        }

        body {
            font-size: 15px !important;
        }

        a[href='../todo.html'], a[href='todo.html'] {
            color: #888 !important;
        }

        .copyright {
            text-align: center;
            margin: 80px 16px 8px 16px;
            padding-top: 16px;
            border-top: #eee 1px solid;
            color: grey;
            font-size: .9em;
        }

        .f-links a{
            text-decoration: underline;
            color: dodgerblue !important;
        }

        .f-links a:hover{
            opacity: .8 !important;
        }

        /*.ad {*/
            /*color: white;*/
            /*text-align: center;*/
            /*background: url(https://img.alicdn.com/tfs/TB1Huu7vMmTBuNjy1XbXXaMrVXa-2880-400.png);*/
            /*background-size: cover;*/
            /*cursor: pointer;*/
            /*margin-bottom: 30px;*/
            /*padding: 18px*/
        /*}*/

        /*.ad:hover {*/
            /*opacity: .8;*/
        /*}*/

        .buy-btn {
            text-decoration: none !important;
            padding: 8px 18px;
            border: #268bd2 1px solid !important;
            display: inline-block !important;
            border-radius: 25px;
            color: #268bd2;
            margin-top: 10px;
        }

        .buy-btn:hover {
            transition: all ease 200ms;
            box-shadow: #555 2px 5px 10px;
            background: #eee;
            color: dodgerblue !important;
        }

        .maoyun{
            padding-top: 5px;
        }


    </style>
</head>
<body>
<!--<div id="ad" style="display: none">-->
<!--<div class="ad ad0"-->
<!--onclick="_track('aliyun','https://promotion.aliyun.com/ntms/yunparter/invite.html?userCode=8hhi4dts')">-->
<!--<div style="font-size: 1.3em; padding-right: 120px; position: relative; top: 12px">阿里云新春特惠</div>-->
<!--<div style="color:orange; font-size: 13px">限时红包 最高￥<span style="font-size: 40px">1888</span></div>-->
<!--</div>-->
<!--<div class="ad ad1"-->
<!--onclick="_track('aliyun-puhui','https://promotion.aliyun.com/ntms/act/qwbk.html?spm=5176.11533457.1089570.10.74e877e33jtaGy&userCode=8hhi4dts')">-->
<!--<div style="font-size: 1.5em;">阿里云全民云计算-新春特惠</div>-->
<!--<div style="color:orange; font-size: 18px;padding-top: 4px;">普惠上云，云服务器1核1G仅需293元/年</div>-->
<!--</div>-->
<!--</div>-->

<div class="ad"  style="text-align:center; padding-bottom:30px; display: none"><a href="https://item.jd.com/12816296.html" onclick="buy('img')" title='点击购买' target="_blank"><img style="height:250px;box-shadow: #aaa 5px 5px 10px;" src="https://pcdn.flutterchina.club/imgs/book.png"/></a>  <br/> <a class="buy-btn" onclick="buy('img')" href="https://item.jd.com/12816296.html" title='点击购买' target="_blank"> 购买实体书 </a></div>


<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="https://flutterchina.club" target="_blank" class="custom-link"><div><img height=20 style='position:relative; top:5px; padding-right:5px ' src='https://cdn.jsdelivr.net/gh/flutterchina/website@1.0/images/favicon.png'/> Flutter中文网</div></a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../intro.html">
            
                <a href="../intro.html">
            
                    
                    前言
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">入门篇</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../chapter1/">
            
                <a href="../chapter1/">
            
                    
                    第一章：起步
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.1" data-path="../chapter1/mobile_development_intro.html">
            
                <a href="../chapter1/mobile_development_intro.html">
            
                    
                    1.1：移动开发技术简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2" data-path="../chapter1/flutter_intro.html">
            
                <a href="../chapter1/flutter_intro.html">
            
                    
                    1.2：初识Flutter
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3" data-path="../chapter1/install_flutter.html">
            
                <a href="../chapter1/install_flutter.html">
            
                    
                    1.3：搭建Flutter开发环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.4" data-path="../chapter1/dart.html">
            
                <a href="../chapter1/dart.html">
            
                    
                    1.4：Dart语言简介
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../chapter2/">
            
                <a href="../chapter2/">
            
                    
                    第二章：第一个Flutter应用
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.2.1" data-path="../chapter2/first_flutter_app.html">
            
                <a href="../chapter2/first_flutter_app.html">
            
                    
                    2.1：计数器示例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2.2" data-path="../chapter2/flutter_router.html">
            
                <a href="../chapter2/flutter_router.html">
            
                    
                    2.2：路由管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2.3" data-path="../chapter2/flutter_package_mgr.html">
            
                <a href="../chapter2/flutter_package_mgr.html">
            
                    
                    2.3：包管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2.4" data-path="../chapter2/flutter_assets_mgr.html">
            
                <a href="../chapter2/flutter_assets_mgr.html">
            
                    
                    2.4：资源管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2.5" data-path="../chapter2/flutter_app_debug.html">
            
                <a href="../chapter2/flutter_app_debug.html">
            
                    
                    2.5：调试Flutter APP
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2.6" data-path="../chapter2/thread_model_and_error_report.html">
            
                <a href="../chapter2/thread_model_and_error_report.html">
            
                    
                    2.6：Dart线程模型及异常捕获
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="./">
            
                <a href="./">
            
                    
                    第三章：基础组件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="2.3.1" data-path="flutter_widget_intro.html">
            
                <a href="flutter_widget_intro.html">
            
                    
                    3.1：Widget简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.2" data-path="state_manage.html">
            
                <a href="state_manage.html">
            
                    
                    3.2：状态管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.3" data-path="text.html">
            
                <a href="text.html">
            
                    
                    3.3：文本、字体样式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.4" data-path="buttons.html">
            
                <a href="buttons.html">
            
                    
                    3.4：按钮
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.5" data-path="img_and_icon.html">
            
                <a href="img_and_icon.html">
            
                    
                    3.5：图片和Icon
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.6" data-path="radio_and_checkbox.html">
            
                <a href="radio_and_checkbox.html">
            
                    
                    3.6：单选框和复选框
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.7" data-path="input_and_form.html">
            
                <a href="input_and_form.html">
            
                    
                    3.7：输入框和表单
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.8" data-path="progress.html">
            
                <a href="progress.html">
            
                    
                    3.8：进度指示器
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../chapter4/">
            
                <a href="../chapter4/">
            
                    
                    第四章：布局类组件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.4.1" data-path="../chapter4/intro.html">
            
                <a href="../chapter4/intro.html">
            
                    
                    4.1：布局类组件简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4.2" data-path="../chapter4/row_and_column.html">
            
                <a href="../chapter4/row_and_column.html">
            
                    
                    4.2：线性布局（Row、Column）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4.3" data-path="../chapter4/flex.html">
            
                <a href="../chapter4/flex.html">
            
                    
                    4.3：弹性布局（Flex）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4.4" data-path="../chapter4/wrap_and_flow.html">
            
                <a href="../chapter4/wrap_and_flow.html">
            
                    
                    4.4：流式布局（Wrap、Flow）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4.5" data-path="../chapter4/stack.html">
            
                <a href="../chapter4/stack.html">
            
                    
                    4.5：层叠布局（Stack、Positioned）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4.6" data-path="../chapter4/alignment.html">
            
                <a href="../chapter4/alignment.html">
            
                    
                    4.6：对齐与相对定位（Align）
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="../chapter5/">
            
                <a href="../chapter5/">
            
                    
                    第五章：容器类组件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.5.1" data-path="../chapter5/padding.html">
            
                <a href="../chapter5/padding.html">
            
                    
                    5.1：填充（Padding）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5.2" data-path="../chapter5/constrainedbox_and_sizebox.html">
            
                <a href="../chapter5/constrainedbox_and_sizebox.html">
            
                    
                    5.2：尺寸限制类容器（ConstrainedBox等）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5.3" data-path="../chapter5/decoratedbox.html">
            
                <a href="../chapter5/decoratedbox.html">
            
                    
                    5.3：装饰容器（DecoratedBox）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5.4" data-path="../chapter5/transform.html">
            
                <a href="../chapter5/transform.html">
            
                    
                    5.4：变换（Transform）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5.5" data-path="../chapter5/container.html">
            
                <a href="../chapter5/container.html">
            
                    
                    5.5：Container容器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5.6" data-path="../chapter5/material_scaffold.html">
            
                <a href="../chapter5/material_scaffold.html">
            
                    
                    5.6：Scaffold、TabBar、底部导航
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5.7" data-path="../chapter5/clip.html">
            
                <a href="../chapter5/clip.html">
            
                    
                    5.7：剪裁（Clip）
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="../chapter6/">
            
                <a href="../chapter6/">
            
                    
                    第六章：可滚动组件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.6.1" data-path="../chapter6/intro.html">
            
                <a href="../chapter6/intro.html">
            
                    
                    6.1：可滚动组件简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6.2" data-path="../chapter6/single_child_scrollview.html">
            
                <a href="../chapter6/single_child_scrollview.html">
            
                    
                    6.2：SingleChildScrollView
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6.3" data-path="../chapter6/listview.html">
            
                <a href="../chapter6/listview.html">
            
                    
                    6.3：ListView
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6.4" data-path="../chapter6/gridview.html">
            
                <a href="../chapter6/gridview.html">
            
                    
                    6.4：GridView
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6.5" data-path="../chapter6/custom_scrollview.html">
            
                <a href="../chapter6/custom_scrollview.html">
            
                    
                    6.5：CustomScrollView
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6.6" data-path="../chapter6/scroll_controller.html">
            
                <a href="../chapter6/scroll_controller.html">
            
                    
                    6.6：滚动监听及控制（ScrollController）
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="../chapter7/">
            
                <a href="../chapter7/">
            
                    
                    第七章：功能型组件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.7.1" data-path="../chapter7/willpopscope.html">
            
                <a href="../chapter7/willpopscope.html">
            
                    
                    7.1：导航返回拦截（WillPopScope）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7.2" data-path="../chapter7/inherited_widget.html">
            
                <a href="../chapter7/inherited_widget.html">
            
                    
                    7.2：数据共享（InheritedWidget）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7.3" data-path="../chapter7/provider.html">
            
                <a href="../chapter7/provider.html">
            
                    
                    7.3： 跨组件状态共享（Provider）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7.4" data-path="../chapter7/theme.html">
            
                <a href="../chapter7/theme.html">
            
                    
                    7.4：颜色和主题（Theme）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7.5" data-path="../chapter7/futurebuilder_and_streambuilder.html">
            
                <a href="../chapter7/futurebuilder_and_streambuilder.html">
            
                    
                    7.5：异步UI更新（FutureBuilder、StreamBuilder）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7.6" data-path="../chapter7/dailog.html">
            
                <a href="../chapter7/dailog.html">
            
                    
                    7.6：对话框详解
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">进阶篇</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../chapter8/">
            
                <a href="../chapter8/">
            
                    
                    第八章：事件处理与通知
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1.1" data-path="../chapter8/listener.html">
            
                <a href="../chapter8/listener.html">
            
                    
                    8.1：原始指针事件处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.2" data-path="../chapter8/gesture.html">
            
                <a href="../chapter8/gesture.html">
            
                    
                    8.2：手势识别
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.3" data-path="../chapter8/eventbus.html">
            
                <a href="../chapter8/eventbus.html">
            
                    
                    8.3：全局事件总线
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.4" data-path="../chapter8/notification.html">
            
                <a href="../chapter8/notification.html">
            
                    
                    8.4：通知(Notification)
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../chapter9/">
            
                <a href="../chapter9/">
            
                    
                    第九章：动画
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.2.1" data-path="../chapter9/intro.html">
            
                <a href="../chapter9/intro.html">
            
                    
                    9.1：Flutter动画简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.2" data-path="../chapter9/animation_structure.html">
            
                <a href="../chapter9/animation_structure.html">
            
                    
                    9.2：动画结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.3" data-path="../chapter9/route_transition.html">
            
                <a href="../chapter9/route_transition.html">
            
                    
                    9.3：自定义路由过渡动画
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.4" data-path="../chapter9/hero.html">
            
                <a href="../chapter9/hero.html">
            
                    
                    9.4：Hero动画
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.5" data-path="../chapter9/stagger_animation.html">
            
                <a href="../chapter9/stagger_animation.html">
            
                    
                    9.5：交织动画
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.6" data-path="../chapter9/animated_switcher.html">
            
                <a href="../chapter9/animated_switcher.html">
            
                    
                    9.6：通用“动画切换”组件（AnimatedSwitcher）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.7" data-path="../chapter9/animated_widgets.html">
            
                <a href="../chapter9/animated_widgets.html">
            
                    
                    9.7：动画过渡组件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../chapter10/">
            
                <a href="../chapter10/">
            
                    
                    第十章：自定义组件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.3.1" data-path="../chapter10/intro.html">
            
                <a href="../chapter10/intro.html">
            
                    
                    10.1：自定义组件方法简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.2" data-path="../chapter10/combine.html">
            
                <a href="../chapter10/combine.html">
            
                    
                    10.2：组合现有组件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.3" data-path="../chapter10/turn_box.html">
            
                <a href="../chapter10/turn_box.html">
            
                    
                    10.3：组合实例：TurnBox
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.4" data-path="../chapter10/custom_paint.html">
            
                <a href="../chapter10/custom_paint.html">
            
                    
                    10.4：自绘组件（CustomPaint与Canvas）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.5" data-path="../chapter10/gradient_circular_progress_demo.html">
            
                <a href="../chapter10/gradient_circular_progress_demo.html">
            
                    
                    10.5：自绘实例：圆形渐变进度条(自绘)
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../chapter11/">
            
                <a href="../chapter11/">
            
                    
                    第十一章：文件操作与网络请求
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.4.1" data-path="../chapter11/file_operation.html">
            
                <a href="../chapter11/file_operation.html">
            
                    
                    11.1：文件操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.2" data-path="../chapter11/http.html">
            
                <a href="../chapter11/http.html">
            
                    
                    11.2：Http请求-HttpClient
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.3" data-path="../chapter11/dio.html">
            
                <a href="../chapter11/dio.html">
            
                    
                    11.3：Http请求-Dio package
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.4" data-path="../chapter11/download_with_chunks.html">
            
                <a href="../chapter11/download_with_chunks.html">
            
                    
                    11.4：实例：Http分块下载
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.5" data-path="../chapter11/websocket.html">
            
                <a href="../chapter11/websocket.html">
            
                    
                    11.5：WebSocket
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.6" data-path="../chapter11/socket.html">
            
                <a href="../chapter11/socket.html">
            
                    
                    11.6：使用Socket API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.7" data-path="../chapter11/json_model.html">
            
                <a href="../chapter11/json_model.html">
            
                    
                    11.7：Json转Dart Model类
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="../chapter12/">
            
                <a href="../chapter12/">
            
                    
                    第十二章：包与插件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.5.1" data-path="../chapter12/develop_package.html">
            
                <a href="../chapter12/develop_package.html">
            
                    
                    12.1：开发package
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5.2" data-path="../chapter12/platform-channel.html">
            
                <a href="../chapter12/platform-channel.html">
            
                    
                    12.2：平台通道简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5.3" data-path="../chapter12/develop_plugin.html">
            
                <a href="../chapter12/develop_plugin.html">
            
                    
                    12.3：开发Flutter插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5.4" data-path="../chapter12/android_implement.html">
            
                <a href="../chapter12/android_implement.html">
            
                    
                    12.4：插件开发：实现Android端API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5.5" data-path="../chapter12/ios_implement.html">
            
                <a href="../chapter12/ios_implement.html">
            
                    
                    12.5：插件开发：实现IOS端API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5.6" data-path="../chapter12/texture_platformview.html">
            
                <a href="../chapter12/texture_platformview.html">
            
                    
                    12.6：Texture和PlatformView
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="../chapter13/">
            
                <a href="../chapter13/">
            
                    
                    第十三章：国际化
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.6.1" data-path="../chapter13/multi_languages_support.html">
            
                <a href="../chapter13/multi_languages_support.html">
            
                    
                    13.1：让App支持多语言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6.2" data-path="../chapter13/locallization_implement.html">
            
                <a href="../chapter13/locallization_implement.html">
            
                    
                    13.2：实现Localizations
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6.3" data-path="../chapter13/intl.html">
            
                <a href="../chapter13/intl.html">
            
                    
                    13.3：使用Intl包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6.4" data-path="../chapter13/faq.html">
            
                <a href="../chapter13/faq.html">
            
                    
                    13.4：国际化常见问题
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="../chapter14/">
            
                <a href="../chapter14/">
            
                    
                    第十四章：Flutter核心原理
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.7.1" data-path="../chapter14/flutter_ui_system.html">
            
                <a href="../chapter14/flutter_ui_system.html">
            
                    
                    14.1：Flutter UI系统
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7.2" data-path="../chapter14/element_buildcontext.html">
            
                <a href="../chapter14/element_buildcontext.html">
            
                    
                    14.2：Element和BuildContext
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7.3" data-path="../chapter14/render_object.html">
            
                <a href="../chapter14/render_object.html">
            
                    
                    14.3：RenderObject与RenderBox
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7.4" data-path="../chapter14/flutter_app_startup.html">
            
                <a href="../chapter14/flutter_app_startup.html">
            
                    
                    14.4：Flutter从启动到显示
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7.5" data-path="../chapter14/image_and_cache.html">
            
                <a href="../chapter14/image_and_cache.html">
            
                    
                    14.5：Flutter图片加载与缓存
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">实例篇</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../chapter15/intro.html">
            
                <a href="../chapter15/intro.html">
            
                    
                    第十五章：一个完整的Flutter应用
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1.1" data-path="../chapter15/intro.html">
            
                <a href="../chapter15/intro.html">
            
                    
                    15.1：应用简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.2" data-path="../chapter15/code_structure.html">
            
                <a href="../chapter15/code_structure.html">
            
                    
                    15.2：APP代码结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.3" data-path="../chapter15/models.html">
            
                <a href="../chapter15/models.html">
            
                    
                    15.3：Model类定义
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.4" data-path="../chapter15/globals.html">
            
                <a href="../chapter15/globals.html">
            
                    
                    15.4：全局变量及共享状态
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.5" data-path="../chapter15/network.html">
            
                <a href="../chapter15/network.html">
            
                    
                    15.5：网络请求封装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.6" data-path="../chapter15/entry.html">
            
                <a href="../chapter15/entry.html">
            
                    
                    15.6：App入口及首页
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.7" data-path="../chapter15/login_page.html">
            
                <a href="../chapter15/login_page.html">
            
                    
                    15.7：登录页
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.8" data-path="../chapter15/language_and_theme_setting.html">
            
                <a href="../chapter15/language_and_theme_setting.html">
            
                    
                    15.8：多语言和多主题
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="https://pcdn.flutterchina.club/gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    
    <a class="btn pull-right" aria-label="GitHub">
        <i class="fa fa-edit" title="编辑本文"></i>
    </a>
    <a class="btn pull-right" aria-label="GitHub" href="https://github.com/flutterchina/flutter-in-action">
        <i class="fa fa-github" title="查看本书开源项目"></i>
    </a>
    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >3.1：Widget简介</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="31-widget&#x7B80;&#x4ECB;">3.1 Widget&#x7B80;&#x4ECB;</h1>
<h2 id="311-&#x6982;&#x5FF5;">3.1.1 &#x6982;&#x5FF5;</h2>
<p>&#x5728;&#x524D;&#x9762;&#x7684;&#x4ECB;&#x7ECD;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x77E5;&#x9053;&#x5728;Flutter&#x4E2D;&#x51E0;&#x4E4E;&#x6240;&#x6709;&#x7684;&#x5BF9;&#x8C61;&#x90FD;&#x662F;&#x4E00;&#x4E2A;Widget&#x3002;&#x4E0E;&#x539F;&#x751F;&#x5F00;&#x53D1;&#x4E2D;&#x201C;&#x63A7;&#x4EF6;&#x201D;&#x4E0D;&#x540C;&#x7684;&#x662F;&#xFF0C;Flutter&#x4E2D;&#x7684;Widget&#x7684;&#x6982;&#x5FF5;&#x66F4;&#x5E7F;&#x6CDB;&#xFF0C;&#x5B83;&#x4E0D;&#x4EC5;&#x53EF;&#x4EE5;&#x8868;&#x793A;UI&#x5143;&#x7D20;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x8868;&#x793A;&#x4E00;&#x4E9B;&#x529F;&#x80FD;&#x6027;&#x7684;&#x7EC4;&#x4EF6;&#x5982;&#xFF1A;&#x7528;&#x4E8E;&#x624B;&#x52BF;&#x68C0;&#x6D4B;&#x7684; <code>GestureDetector</code> widget&#x3001;&#x7528;&#x4E8E;APP&#x4E3B;&#x9898;&#x6570;&#x636E;&#x4F20;&#x9012;&#x7684;<code>Theme</code>&#x7B49;&#x7B49;&#xFF0C;&#x800C;&#x539F;&#x751F;&#x5F00;&#x53D1;&#x4E2D;&#x7684;&#x63A7;&#x4EF6;&#x901A;&#x5E38;&#x53EA;&#x662F;&#x6307;UI&#x5143;&#x7D20;&#x3002;&#x5728;&#x540E;&#x9762;&#x7684;&#x5185;&#x5BB9;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5728;&#x63CF;&#x8FF0;UI&#x5143;&#x7D20;&#x65F6;&#x53EF;&#x80FD;&#x4F1A;&#x7528;&#x5230;&#x201C;&#x63A7;&#x4EF6;&#x201D;&#x3001;&#x201C;&#x7EC4;&#x4EF6;&#x201D;&#x8FD9;&#x6837;&#x7684;&#x6982;&#x5FF5;&#xFF0C;&#x8BFB;&#x8005;&#x5FC3;&#x91CC;&#x9700;&#x8981;&#x77E5;&#x9053;&#x4ED6;&#x4EEC;&#x5C31;&#x662F;widget&#xFF0C;&#x53EA;&#x662F;&#x5728;&#x4E0D;&#x540C;&#x573A;&#x666F;&#x7684;&#x4E0D;&#x540C;&#x8868;&#x8FF0;&#x800C;&#x5DF2;&#x3002;&#x7531;&#x4E8E;Flutter&#x4E3B;&#x8981;&#x5C31;&#x662F;&#x7528;&#x4E8E;&#x6784;&#x5EFA;&#x7528;&#x6237;&#x754C;&#x9762;&#x7684;&#xFF0C;&#x6240;&#x4EE5;&#xFF0C;&#x5728;&#x5927;&#x591A;&#x6570;&#x65F6;&#x5019;&#xFF0C;&#x8BFB;&#x8005;&#x53EF;&#x4EE5;&#x8BA4;&#x4E3A;widget&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x63A7;&#x4EF6;&#xFF0C;&#x4E0D;&#x5FC5;&#x7EA0;&#x7ED3;&#x4E8E;&#x6982;&#x5FF5;&#x3002;</p>
<h2 id="312-widget&#x4E0E;element">3.1.2 Widget&#x4E0E;Element</h2>
<p>&#x5728;Flutter&#x4E2D;&#xFF0C;Widget&#x7684;&#x529F;&#x80FD;&#x662F;&#x201C;&#x63CF;&#x8FF0;&#x4E00;&#x4E2A;UI&#x5143;&#x7D20;&#x7684;&#x914D;&#x7F6E;&#x6570;&#x636E;&#x201D;&#xFF0C;&#x5B83;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;Widget&#x5176;&#x5B9E;&#x5E76;&#x4E0D;&#x662F;&#x8868;&#x793A;&#x6700;&#x7EC8;&#x7ED8;&#x5236;&#x5728;&#x8BBE;&#x5907;&#x5C4F;&#x5E55;&#x4E0A;&#x7684;&#x663E;&#x793A;&#x5143;&#x7D20;&#xFF0C;&#x800C;&#x5B83;&#x53EA;&#x662F;&#x63CF;&#x8FF0;&#x663E;&#x793A;&#x5143;&#x7D20;&#x7684;&#x4E00;&#x4E2A;&#x914D;&#x7F6E;&#x6570;&#x636E;&#x3002;</p>
<p>&#x5B9E;&#x9645;&#x4E0A;&#xFF0C;Flutter&#x4E2D;&#x771F;&#x6B63;&#x4EE3;&#x8868;&#x5C4F;&#x5E55;&#x4E0A;&#x663E;&#x793A;&#x5143;&#x7D20;&#x7684;&#x7C7B;&#x662F;<code>Element</code>&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;Widget&#x53EA;&#x662F;&#x63CF;&#x8FF0;<code>Element</code>&#x7684;&#x914D;&#x7F6E;&#x6570;&#x636E;&#xFF01;&#x6709;&#x5173;<code>Element</code>&#x7684;&#x8BE6;&#x7EC6;&#x4ECB;&#x7ECD;&#x6211;&#x4EEC;&#x5C06;&#x5728;&#x672C;&#x4E66;&#x540E;&#x9762;&#x7684;&#x9AD8;&#x7EA7;&#x90E8;&#x5206;&#x6DF1;&#x5165;&#x4ECB;&#x7ECD;&#xFF0C;&#x73B0;&#x5728;&#xFF0C;&#x8BFB;&#x8005;&#x53EA;&#x9700;&#x8981;&#x77E5;&#x9053;&#xFF1A;<strong>Widget&#x53EA;&#x662F;UI&#x5143;&#x7D20;&#x7684;&#x4E00;&#x4E2A;&#x914D;&#x7F6E;&#x6570;&#x636E;&#xFF0C;&#x5E76;&#x4E14;&#x4E00;&#x4E2A;Widget&#x53EF;&#x4EE5;&#x5BF9;&#x5E94;&#x591A;&#x4E2A;<code>Element</code></strong>&#x3002;&#x8FD9;&#x662F;&#x56E0;&#x4E3A;&#x540C;&#x4E00;&#x4E2A;Widget&#x5BF9;&#x8C61;&#x53EF;&#x4EE5;&#x88AB;&#x6DFB;&#x52A0;&#x5230;UI&#x6811;&#x7684;&#x4E0D;&#x540C;&#x90E8;&#x5206;&#xFF0C;&#x800C;&#x771F;&#x6B63;&#x6E32;&#x67D3;&#x65F6;&#xFF0C;UI&#x6811;&#x7684;&#x6BCF;&#x4E00;&#x4E2A;<code>Element</code>&#x8282;&#x70B9;&#x90FD;&#x4F1A;&#x5BF9;&#x5E94;&#x4E00;&#x4E2A;Widget&#x5BF9;&#x8C61;&#x3002;&#x603B;&#x7ED3;&#x4E00;&#x4E0B;&#xFF1A;</p>
<ul>
<li>Widget&#x5B9E;&#x9645;&#x4E0A;&#x5C31;&#x662F;<code>Element</code>&#x7684;&#x914D;&#x7F6E;&#x6570;&#x636E;&#xFF0C;Widget&#x6811;&#x5B9E;&#x9645;&#x4E0A;&#x662F;&#x4E00;&#x4E2A;&#x914D;&#x7F6E;&#x6811;&#xFF0C;&#x800C;&#x771F;&#x6B63;&#x7684;UI&#x6E32;&#x67D3;&#x6811;&#x662F;&#x7531;<code>Element</code>&#x6784;&#x6210;&#xFF1B;&#x4E0D;&#x8FC7;&#xFF0C;&#x7531;&#x4E8E;<code>Element</code>&#x662F;&#x901A;&#x8FC7;Widget&#x751F;&#x6210;&#x7684;&#xFF0C;&#x6240;&#x4EE5;&#x5B83;&#x4EEC;&#x4E4B;&#x95F4;&#x6709;&#x5BF9;&#x5E94;&#x5173;&#x7CFB;&#xFF0C;&#x5728;&#x5927;&#x591A;&#x6570;&#x573A;&#x666F;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5BBD;&#x6CDB;&#x5730;&#x8BA4;&#x4E3A;Widget&#x6811;&#x5C31;&#x662F;&#x6307;UI&#x63A7;&#x4EF6;&#x6811;&#x6216;UI&#x6E32;&#x67D3;&#x6811;&#x3002;</li>
<li>&#x4E00;&#x4E2A;Widget&#x5BF9;&#x8C61;&#x53EF;&#x4EE5;&#x5BF9;&#x5E94;&#x591A;&#x4E2A;<code>Element</code>&#x5BF9;&#x8C61;&#x3002;&#x8FD9;&#x5F88;&#x597D;&#x7406;&#x89E3;&#xFF0C;&#x6839;&#x636E;&#x540C;&#x4E00;&#x4EFD;&#x914D;&#x7F6E;&#xFF08;Widget&#xFF09;&#xFF0C;&#x53EF;&#x4EE5;&#x521B;&#x5EFA;&#x591A;&#x4E2A;&#x5B9E;&#x4F8B;&#xFF08;Element&#xFF09;&#x3002;</li>
</ul>
<p>&#x8BFB;&#x8005;&#x5E94;&#x8BE5;&#x5C06;&#x8FD9;&#x4E24;&#x70B9;&#x7262;&#x8BB0;&#x5728;&#x5FC3;&#x4E2D;&#x3002;</p>
<h2 id="313-widget&#x4E3B;&#x8981;&#x63A5;&#x53E3;">3.1.3 Widget&#x4E3B;&#x8981;&#x63A5;&#x53E3;</h2>
<p>&#x6211;&#x4EEC;&#x5148;&#x6765;&#x770B;&#x4E00;&#x4E0B;Widget&#x7C7B;&#x7684;&#x58F0;&#x660E;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"><span class="token metadata symbol">@immutable</span>
<span class="token keyword">abstract</span> <span class="token keyword">class</span> <span class="token class-name">Widget</span> <span class="token keyword">extends</span> <span class="token class-name">DiagnosticableTree</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> <span class="token function">Widget</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>key <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">final</span> Key key<span class="token punctuation">;</span>

  <span class="token metadata symbol">@protected</span>
  Element <span class="token function">createElement</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token metadata symbol">@override</span>
  String <span class="token function">toStringShort</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> key <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">?</span> <span class="token string">&apos;$runtimeType&apos;</span> <span class="token punctuation">:</span> <span class="token string">&apos;$runtimeType-$key&apos;</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token metadata symbol">@override</span>
  <span class="token keyword">void</span> <span class="token function">debugFillProperties</span><span class="token punctuation">(</span>DiagnosticPropertiesBuilder properties<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">debugFillProperties</span><span class="token punctuation">(</span>properties<span class="token punctuation">)</span><span class="token punctuation">;</span>
    properties<span class="token punctuation">.</span>defaultDiagnosticsTreeStyle <span class="token operator">=</span> DiagnosticsTreeStyle<span class="token punctuation">.</span>dense<span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token keyword">static</span> bool <span class="token function">canUpdate</span><span class="token punctuation">(</span>Widget oldWidget<span class="token punctuation">,</span> Widget newWidget<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> oldWidget<span class="token punctuation">.</span>runtimeType <span class="token operator">==</span> newWidget<span class="token punctuation">.</span>runtimeType
        <span class="token operator">&amp;&amp;</span> oldWidget<span class="token punctuation">.</span>key <span class="token operator">==</span> newWidget<span class="token punctuation">.</span>key<span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<ul>
<li><code>Widget</code>&#x7C7B;&#x7EE7;&#x627F;&#x81EA;<code>DiagnosticableTree</code>&#xFF0C;<code>DiagnosticableTree</code>&#x5373;&#x201C;&#x8BCA;&#x65AD;&#x6811;&#x201D;&#xFF0C;&#x4E3B;&#x8981;&#x4F5C;&#x7528;&#x662F;&#x63D0;&#x4F9B;&#x8C03;&#x8BD5;&#x4FE1;&#x606F;&#x3002;</li>
<li><code>Key</code>: &#x8FD9;&#x4E2A;<code>key</code>&#x5C5E;&#x6027;&#x7C7B;&#x4F3C;&#x4E8E;React/Vue&#x4E2D;&#x7684;<code>key</code>&#xFF0C;&#x4E3B;&#x8981;&#x7684;&#x4F5C;&#x7528;&#x662F;&#x51B3;&#x5B9A;&#x662F;&#x5426;&#x5728;&#x4E0B;&#x4E00;&#x6B21;<code>build</code>&#x65F6;&#x590D;&#x7528;&#x65E7;&#x7684;widget&#xFF0C;&#x51B3;&#x5B9A;&#x7684;&#x6761;&#x4EF6;&#x5728;<code>canUpdate()</code>&#x65B9;&#x6CD5;&#x4E2D;&#x3002;</li>
<li><code>createElement()</code>&#xFF1A;&#x6B63;&#x5982;&#x524D;&#x6587;&#x6240;&#x8FF0;&#x201C;&#x4E00;&#x4E2A;Widget&#x53EF;&#x4EE5;&#x5BF9;&#x5E94;&#x591A;&#x4E2A;<code>Element</code>&#x201D;&#xFF1B;Flutter Framework&#x5728;&#x6784;&#x5EFA;UI&#x6811;&#x65F6;&#xFF0C;&#x4F1A;&#x5148;&#x8C03;&#x7528;&#x6B64;&#x65B9;&#x6CD5;&#x751F;&#x6210;&#x5BF9;&#x5E94;&#x8282;&#x70B9;&#x7684;<code>Element</code>&#x5BF9;&#x8C61;&#x3002;&#x6B64;&#x65B9;&#x6CD5;&#x662F;Flutter Framework&#x9690;&#x5F0F;&#x8C03;&#x7528;&#x7684;&#xFF0C;&#x5728;&#x6211;&#x4EEC;&#x5F00;&#x53D1;&#x8FC7;&#x7A0B;&#x4E2D;&#x57FA;&#x672C;&#x4E0D;&#x4F1A;&#x8C03;&#x7528;&#x5230;&#x3002;</li>
<li><code>debugFillProperties(...)</code> &#x590D;&#x5199;&#x7236;&#x7C7B;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x4E3B;&#x8981;&#x662F;&#x8BBE;&#x7F6E;&#x8BCA;&#x65AD;&#x6811;&#x7684;&#x4E00;&#x4E9B;&#x7279;&#x6027;&#x3002;</li>
<li><code>canUpdate(...)</code>&#x662F;&#x4E00;&#x4E2A;&#x9759;&#x6001;&#x65B9;&#x6CD5;&#xFF0C;&#x5B83;&#x4E3B;&#x8981;&#x7528;&#x4E8E;&#x5728;Widget&#x6811;&#x91CD;&#x65B0;<code>build</code>&#x65F6;&#x590D;&#x7528;&#x65E7;&#x7684;widget&#xFF0C;&#x5176;&#x5B9E;&#x5177;&#x4F53;&#x6765;&#x8BF4;&#xFF0C;&#x5E94;&#x8BE5;&#x662F;&#xFF1A;&#x662F;&#x5426;&#x7528;&#x65B0;&#x7684;Widget&#x5BF9;&#x8C61;&#x53BB;&#x66F4;&#x65B0;&#x65E7;UI&#x6811;&#x4E0A;&#x6240;&#x5BF9;&#x5E94;&#x7684;<code>Element</code>&#x5BF9;&#x8C61;&#x7684;&#x914D;&#x7F6E;&#xFF1B;&#x901A;&#x8FC7;&#x5176;&#x6E90;&#x7801;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#xFF0C;&#x53EA;&#x8981;<code>newWidget</code>&#x4E0E;<code>oldWidget</code>&#x7684;<code>runtimeType</code>&#x548C;<code>key</code>&#x540C;&#x65F6;&#x76F8;&#x7B49;&#x65F6;&#x5C31;&#x4F1A;&#x7528;<code>newWidget</code>&#x53BB;&#x66F4;&#x65B0;<code>Element</code>&#x5BF9;&#x8C61;&#x7684;&#x914D;&#x7F6E;&#xFF0C;&#x5426;&#x5219;&#x5C31;&#x4F1A;&#x521B;&#x5EFA;&#x65B0;&#x7684;<code>Element</code>&#x3002;</li>
</ul>
<p>&#x6709;&#x5173;Key&#x548C;Widget&#x590D;&#x7528;&#x7684;&#x7EC6;&#x8282;&#x5C06;&#x4F1A;&#x5728;&#x672C;&#x4E66;&#x540E;&#x9762;&#x9AD8;&#x7EA7;&#x90E8;&#x5206;&#x6DF1;&#x5165;&#x8BA8;&#x8BBA;&#xFF0C;&#x8BFB;&#x8005;&#x73B0;&#x5728;&#x53EA;&#x9700;&#x77E5;&#x9053;&#xFF0C;&#x4E3A;Widget&#x663E;&#x5F0F;&#x6DFB;&#x52A0;key&#x7684;&#x8BDD;&#x53EF;&#x80FD;&#xFF08;&#x4F46;&#x4E0D;&#x4E00;&#x5B9A;&#xFF09;&#x4F1A;&#x4F7F;UI&#x5728;&#x91CD;&#x65B0;&#x6784;&#x5EFA;&#x65F6;&#x53D8;&#x7684;&#x9AD8;&#x6548;&#xFF0C;&#x8BFB;&#x8005;&#x76EE;&#x524D;&#x53EF;&#x4EE5;&#x5148;&#x5FFD;&#x7565;&#x6B64;&#x53C2;&#x6570;&#x3002;&#x672C;&#x4E66;&#x540E;&#x9762;&#x7684;&#x793A;&#x4F8B;&#x4E2D;&#xFF0C;&#x53EA;&#x4F1A;&#x5728;&#x6784;&#x5EFA;&#x5217;&#x8868;&#x9879;UI&#x65F6;&#x4F1A;&#x663E;&#x5F0F;&#x6307;&#x5B9A;Key&#x3002;</p>
<p>&#x53E6;&#x5916;<code>Widget</code>&#x7C7B;&#x672C;&#x8EAB;&#x662F;&#x4E00;&#x4E2A;&#x62BD;&#x8C61;&#x7C7B;&#xFF0C;&#x5176;&#x4E2D;&#x6700;&#x6838;&#x5FC3;&#x7684;&#x5C31;&#x662F;&#x5B9A;&#x4E49;&#x4E86;<code>createElement()</code>&#x63A5;&#x53E3;&#xFF0C;&#x5728;Flutter&#x5F00;&#x53D1;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x4E00;&#x822C;&#x90FD;&#x4E0D;&#x7528;&#x76F4;&#x63A5;&#x7EE7;&#x627F;<code>Widget</code>&#x7C7B;&#x6765;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x65B0;&#x7EC4;&#x4EF6;&#xFF0C;&#x76F8;&#x53CD;&#xFF0C;&#x6211;&#x4EEC;&#x901A;&#x5E38;&#x4F1A;&#x901A;&#x8FC7;&#x7EE7;&#x627F;<code>StatelessWidget</code>&#x6216;<code>StatefulWidget</code>&#x6765;&#x95F4;&#x63A5;&#x7EE7;&#x627F;<code>Widget</code>&#x7C7B;&#x6765;&#x5B9E;&#x73B0;&#x3002;<code>StatelessWidget</code>&#x548C;<code>StatefulWidget</code>&#x90FD;&#x662F;&#x76F4;&#x63A5;&#x7EE7;&#x627F;&#x81EA;<code>Widget</code>&#x7C7B;&#xFF0C;&#x800C;&#x8FD9;&#x4E24;&#x4E2A;&#x7C7B;&#x4E5F;&#x6B63;&#x662F;Flutter&#x4E2D;&#x975E;&#x5E38;&#x91CD;&#x8981;&#x7684;&#x4E24;&#x4E2A;&#x62BD;&#x8C61;&#x7C7B;&#xFF0C;&#x5B83;&#x4EEC;&#x5F15;&#x5165;&#x4E86;&#x4E24;&#x79CD;Widget&#x6A21;&#x578B;&#xFF0C;&#x63A5;&#x4E0B;&#x6765;&#x6211;&#x4EEC;&#x5C06;&#x91CD;&#x70B9;&#x4ECB;&#x7ECD;&#x4E00;&#x4E0B;&#x8FD9;&#x4E24;&#x4E2A;&#x7C7B;&#x3002;</p>
<h2 id="314-statelesswidget">3.1.4 StatelessWidget</h2>
<p>&#x5728;&#x4E4B;&#x524D;&#x7684;&#x7AE0;&#x8282;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x7B80;&#x5355;&#x4ECB;&#x7ECD;&#x8FC7;<code>StatelessWidget</code>&#xFF0C;<code>StatelessWidget</code>&#x76F8;&#x5BF9;&#x6BD4;&#x8F83;&#x7B80;&#x5355;&#xFF0C;&#x5B83;&#x7EE7;&#x627F;&#x81EA;<code>Widget</code>&#x7C7B;&#xFF0C;&#x91CD;&#x5199;&#x4E86;<code>createElement()</code>&#x65B9;&#x6CD5;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"><span class="token metadata symbol">@override</span>
StatelessElement <span class="token function">createElement</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token keyword">new</span> <span class="token class-name">StatelessElement</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<p><code>StatelessElement</code> &#x95F4;&#x63A5;&#x7EE7;&#x627F;&#x81EA;<code>Element</code>&#x7C7B;&#xFF0C;&#x4E0E;<code>StatelessWidget</code>&#x76F8;&#x5BF9;&#x5E94;&#xFF08;&#x4F5C;&#x4E3A;&#x5176;&#x914D;&#x7F6E;&#x6570;&#x636E;&#xFF09;&#x3002;</p>
<p><code>StatelessWidget</code>&#x7528;&#x4E8E;&#x4E0D;&#x9700;&#x8981;&#x7EF4;&#x62A4;&#x72B6;&#x6001;&#x7684;&#x573A;&#x666F;&#xFF0C;&#x5B83;&#x901A;&#x5E38;&#x5728;<code>build</code>&#x65B9;&#x6CD5;&#x4E2D;&#x901A;&#x8FC7;&#x5D4C;&#x5957;&#x5176;&#x5B83;Widget&#x6765;&#x6784;&#x5EFA;UI&#xFF0C;&#x5728;&#x6784;&#x5EFA;&#x8FC7;&#x7A0B;&#x4E2D;&#x4F1A;&#x9012;&#x5F52;&#x7684;&#x6784;&#x5EFA;&#x5176;&#x5D4C;&#x5957;&#x7684;Widget&#x3002;&#x6211;&#x4EEC;&#x770B;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"><span class="token keyword">class</span> <span class="token class-name">Echo</span> <span class="token keyword">extends</span> <span class="token class-name">StatelessWidget</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> <span class="token function">Echo</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
    Key key<span class="token punctuation">,</span>  
    <span class="token metadata symbol">@required</span> <span class="token keyword">this</span><span class="token punctuation">.</span>text<span class="token punctuation">,</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>backgroundColor<span class="token punctuation">:</span>Colors<span class="token punctuation">.</span>grey<span class="token punctuation">,</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">:</span><span class="token keyword">super</span><span class="token punctuation">(</span>key<span class="token punctuation">:</span>key<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword">final</span> String text<span class="token punctuation">;</span>
  <span class="token keyword">final</span> Color backgroundColor<span class="token punctuation">;</span>

  <span class="token metadata symbol">@override</span>
  Widget <span class="token function">build</span><span class="token punctuation">(</span>BuildContext context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token function">Center</span><span class="token punctuation">(</span>
      child<span class="token punctuation">:</span> <span class="token function">Container</span><span class="token punctuation">(</span>
        color<span class="token punctuation">:</span> backgroundColor<span class="token punctuation">,</span>
        child<span class="token punctuation">:</span> <span class="token function">Text</span><span class="token punctuation">(</span>text<span class="token punctuation">)</span><span class="token punctuation">,</span>
      <span class="token punctuation">)</span><span class="token punctuation">,</span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x4E0A;&#x9762;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x5B9E;&#x73B0;&#x4E86;&#x4E00;&#x4E2A;&#x56DE;&#x663E;&#x5B57;&#x7B26;&#x4E32;&#x7684;<code>Echo</code> widget&#x3002;</p>
<blockquote>
<p>&#x6309;&#x7167;&#x60EF;&#x4F8B;&#xFF0C;<code>widget</code>&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x53C2;&#x6570;&#x5E94;&#x4F7F;&#x7528;&#x547D;&#x540D;&#x53C2;&#x6570;&#xFF0C;&#x547D;&#x540D;&#x53C2;&#x6570;&#x4E2D;&#x7684;&#x5FC5;&#x8981;&#x53C2;&#x6570;&#x8981;&#x6DFB;&#x52A0;<code>@required</code>&#x6807;&#x6CE8;&#xFF0C;&#x8FD9;&#x6837;&#x6709;&#x5229;&#x4E8E;&#x9759;&#x6001;&#x4EE3;&#x7801;&#x5206;&#x6790;&#x5668;&#x8FDB;&#x884C;&#x68C0;&#x67E5;&#x3002;&#x53E6;&#x5916;&#xFF0C;&#x5728;&#x7EE7;&#x627F;<code>widget</code>&#x65F6;&#xFF0C;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x901A;&#x5E38;&#x5E94;&#x8BE5;&#x662F;<code>Key</code>&#xFF0C;&#x53E6;&#x5916;&#xFF0C;&#x5982;&#x679C;Widget&#x9700;&#x8981;&#x63A5;&#x6536;&#x5B50;Widget&#xFF0C;&#x90A3;&#x4E48;<code>child</code>&#x6216;<code>children</code>&#x53C2;&#x6570;&#x901A;&#x5E38;&#x5E94;&#x88AB;&#x653E;&#x5728;&#x53C2;&#x6570;&#x5217;&#x8868;&#x7684;&#x6700;&#x540E;&#x3002;&#x540C;&#x6837;&#x662F;&#x6309;&#x7167;&#x60EF;&#x4F8B;&#xFF0C;Widget&#x7684;&#x5C5E;&#x6027;&#x5E94;&#x5C3D;&#x53EF;&#x80FD;&#x7684;&#x88AB;&#x58F0;&#x660E;&#x4E3A;<code>final</code>&#xFF0C;&#x9632;&#x6B62;&#x88AB;&#x610F;&#x5916;&#x6539;&#x53D8;&#x3002;</p>
</blockquote>
<p>&#x7136;&#x540E;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x5982;&#x4E0B;&#x65B9;&#x5F0F;&#x4F7F;&#x7528;&#x5B83;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart">Widget <span class="token function">build</span><span class="token punctuation">(</span>BuildContext context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token function">Echo</span><span class="token punctuation">(</span>text<span class="token punctuation">:</span> <span class="token string">&quot;hello world&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x8FD0;&#x884C;&#x540E;&#x6548;&#x679C;&#x5982;&#x56FE;3-1&#x6240;&#x793A;&#xFF1A;</p>
<p><img src="https://pcdn.flutterchina.club/imgs/3-1.png" alt="&#x56FE;3-1"></p>
<h3 id="context">Context</h3>
<p><code>build</code>&#x65B9;&#x6CD5;&#x6709;&#x4E00;&#x4E2A;<code>context</code>&#x53C2;&#x6570;&#xFF0C;&#x5B83;&#x662F;<code>BuildContext</code>&#x7C7B;&#x7684;&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;&#xFF0C;&#x8868;&#x793A;&#x5F53;&#x524D;widget&#x5728;widget&#x6811;&#x4E2D;&#x7684;&#x4E0A;&#x4E0B;&#x6587;&#xFF0C;&#x6BCF;&#x4E00;&#x4E2A;widget&#x90FD;&#x4F1A;&#x5BF9;&#x5E94;&#x4E00;&#x4E2A;context&#x5BF9;&#x8C61;&#xFF08;&#x56E0;&#x4E3A;&#x6BCF;&#x4E00;&#x4E2A;widget&#x90FD;&#x662F;widget&#x6811;&#x4E0A;&#x7684;&#x4E00;&#x4E2A;&#x8282;&#x70B9;&#xFF09;&#x3002;&#x5B9E;&#x9645;&#x4E0A;&#xFF0C;<code>context</code>&#x662F;&#x5F53;&#x524D;widget&#x5728;widget&#x6811;&#x4E2D;&#x4F4D;&#x7F6E;&#x4E2D;&#x6267;&#x884C;&#x201D;&#x76F8;&#x5173;&#x64CD;&#x4F5C;&#x201C;&#x7684;&#x4E00;&#x4E2A;&#x53E5;&#x67C4;&#xFF0C;&#x6BD4;&#x5982;&#x5B83;&#x63D0;&#x4F9B;&#x4E86;&#x4ECE;&#x5F53;&#x524D;widget&#x5F00;&#x59CB;&#x5411;&#x4E0A;&#x904D;&#x5386;widget&#x6811;&#x4EE5;&#x53CA;&#x6309;&#x7167;widget&#x7C7B;&#x578B;&#x67E5;&#x627E;&#x7236;&#x7EA7;widget&#x7684;&#x65B9;&#x6CD5;&#x3002;&#x4E0B;&#x9762;&#x662F;&#x5728;&#x5B50;&#x6811;&#x4E2D;&#x83B7;&#x53D6;&#x7236;&#x7EA7;widget&#x7684;&#x4E00;&#x4E2A;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"><span class="token keyword">class</span> <span class="token class-name">ContextRoute</span> <span class="token keyword">extends</span> <span class="token class-name">StatelessWidget</span> <span class="token punctuation">{</span>
  <span class="token metadata symbol">@override</span>
  Widget <span class="token function">build</span><span class="token punctuation">(</span>BuildContext context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token function">Scaffold</span><span class="token punctuation">(</span>
      appBar<span class="token punctuation">:</span> <span class="token function">AppBar</span><span class="token punctuation">(</span>
        title<span class="token punctuation">:</span> <span class="token function">Text</span><span class="token punctuation">(</span><span class="token string">&quot;Context&#x6D4B;&#x8BD5;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
      <span class="token punctuation">)</span><span class="token punctuation">,</span>
      body<span class="token punctuation">:</span> <span class="token function">Container</span><span class="token punctuation">(</span>
        child<span class="token punctuation">:</span> <span class="token function">Builder</span><span class="token punctuation">(</span>builder<span class="token punctuation">:</span> <span class="token punctuation">(</span>context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
          <span class="token comment">// &#x5728;Widget&#x6811;&#x4E2D;&#x5411;&#x4E0A;&#x67E5;&#x627E;&#x6700;&#x8FD1;&#x7684;&#x7236;&#x7EA7;`Scaffold` widget</span>
          Scaffold scaffold <span class="token operator">=</span> context<span class="token punctuation">.</span><span class="token function">ancestorWidgetOfExactType</span><span class="token punctuation">(</span>Scaffold<span class="token punctuation">)</span><span class="token punctuation">;</span>
          <span class="token comment">// &#x76F4;&#x63A5;&#x8FD4;&#x56DE; AppBar&#x7684;title&#xFF0C; &#x6B64;&#x5904;&#x5B9E;&#x9645;&#x4E0A;&#x662F;Text(&quot;Context&#x6D4B;&#x8BD5;&quot;)</span>
          <span class="token keyword">return</span> <span class="token punctuation">(</span>scaffold<span class="token punctuation">.</span>appBar <span class="token operator">as</span> AppBar<span class="token punctuation">)</span><span class="token punctuation">.</span>title<span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
      <span class="token punctuation">)</span><span class="token punctuation">,</span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x8FD0;&#x884C;&#x540E;&#x6548;&#x679C;&#x5982;&#x56FE;3-1-1&#x6240;&#x793A;&#xFF1A;</p>
<p><img src="https://pcdn.flutterchina.club/imgs/3-1-1.png" alt="&#x56FE;3-1-1"></p>
<blockquote>
<p><strong>&#x6CE8;&#x610F;</strong>&#xFF1A;&#x5BF9;&#x4E8E;<code>BuildContext</code>&#x8BFB;&#x8005;&#x73B0;&#x5728;&#x53EF;&#x4EE5;&#x5148;&#x4F5C;&#x4E86;&#x89E3;&#xFF0C;&#x968F;&#x7740;&#x672C;&#x4E66;&#x540E;&#x9762;&#x5185;&#x5BB9;&#x7684;&#x5C55;&#x5F00;&#xFF0C;&#x4E5F;&#x4F1A;&#x7528;&#x5230;Context&#x7684;&#x4E00;&#x4E9B;&#x65B9;&#x6CD5;&#xFF0C;&#x8BFB;&#x8005;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x5177;&#x4F53;&#x7684;&#x573A;&#x666F;&#x5BF9;&#x5176;&#x6709;&#x4E2A;&#x76F4;&#x89C2;&#x7684;&#x8BA4;&#x8BC6;&#x3002;&#x5173;&#x4E8E;<code>BuildContext</code>&#x66F4;&#x591A;&#x7684;&#x5185;&#x5BB9;&#xFF0C;&#x6211;&#x4EEC;&#x4E5F;&#x5C06;&#x5728;&#x540E;&#x9762;&#x9AD8;&#x7EA7;&#x90E8;&#x5206;&#x518D;&#x6DF1;&#x5165;&#x4ECB;&#x7ECD;&#x3002;</p>
</blockquote>
<h2 id="315-statefulwidget">3.1.5 StatefulWidget</h2>
<p>&#x548C;<code>StatelessWidget</code>&#x4E00;&#x6837;&#xFF0C;<code>StatefulWidget</code>&#x4E5F;&#x662F;&#x7EE7;&#x627F;&#x81EA;<code>Widget</code>&#x7C7B;&#xFF0C;&#x5E76;&#x91CD;&#x5199;&#x4E86;<code>createElement()</code>&#x65B9;&#x6CD5;&#xFF0C;&#x4E0D;&#x540C;&#x7684;&#x662F;&#x8FD4;&#x56DE;&#x7684;<code>Element</code> &#x5BF9;&#x8C61;&#x5E76;&#x4E0D;&#x76F8;&#x540C;&#xFF1B;&#x53E6;&#x5916;<code>StatefulWidget</code>&#x7C7B;&#x4E2D;&#x6DFB;&#x52A0;&#x4E86;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x63A5;&#x53E3;<code>createState()</code>&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x6211;&#x4EEC;&#x770B;&#x770B;<code>StatefulWidget</code>&#x7684;&#x7C7B;&#x5B9A;&#x4E49;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"><span class="token keyword">abstract</span> <span class="token keyword">class</span> <span class="token class-name">StatefulWidget</span> <span class="token keyword">extends</span> <span class="token class-name">Widget</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> <span class="token function">StatefulWidget</span><span class="token punctuation">(</span><span class="token punctuation">{</span> Key key <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token keyword">super</span><span class="token punctuation">(</span>key<span class="token punctuation">:</span> key<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token metadata symbol">@override</span>
  StatefulElement <span class="token function">createElement</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token keyword">new</span> <span class="token class-name">StatefulElement</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token metadata symbol">@protected</span>
  State <span class="token function">createState</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<ul>
<li><p><code>StatefulElement</code> &#x95F4;&#x63A5;&#x7EE7;&#x627F;&#x81EA;<code>Element</code>&#x7C7B;&#xFF0C;&#x4E0E;StatefulWidget&#x76F8;&#x5BF9;&#x5E94;&#xFF08;&#x4F5C;&#x4E3A;&#x5176;&#x914D;&#x7F6E;&#x6570;&#x636E;&#xFF09;&#x3002;<code>StatefulElement</code>&#x4E2D;&#x53EF;&#x80FD;&#x4F1A;&#x591A;&#x6B21;&#x8C03;&#x7528;<code>createState()</code>&#x6765;&#x521B;&#x5EFA;&#x72B6;&#x6001;(State)&#x5BF9;&#x8C61;&#x3002;</p>
</li>
<li><p><code>createState()</code> &#x7528;&#x4E8E;&#x521B;&#x5EFA;&#x548C;Stateful widget&#x76F8;&#x5173;&#x7684;&#x72B6;&#x6001;&#xFF0C;&#x5B83;&#x5728;Stateful widget&#x7684;&#x751F;&#x547D;&#x5468;&#x671F;&#x4E2D;&#x53EF;&#x80FD;&#x4F1A;&#x88AB;&#x591A;&#x6B21;&#x8C03;&#x7528;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x5F53;&#x4E00;&#x4E2A;Stateful widget&#x540C;&#x65F6;&#x63D2;&#x5165;&#x5230;widget&#x6811;&#x7684;&#x591A;&#x4E2A;&#x4F4D;&#x7F6E;&#x65F6;&#xFF0C;Flutter framework&#x5C31;&#x4F1A;&#x8C03;&#x7528;&#x8BE5;&#x65B9;&#x6CD5;&#x4E3A;&#x6BCF;&#x4E00;&#x4E2A;&#x4F4D;&#x7F6E;&#x751F;&#x6210;&#x4E00;&#x4E2A;&#x72EC;&#x7ACB;&#x7684;State&#x5B9E;&#x4F8B;&#xFF0C;&#x5176;&#x5B9E;&#xFF0C;&#x672C;&#x8D28;&#x4E0A;&#x5C31;&#x662F;&#x4E00;&#x4E2A;<code>StatefulElement</code>&#x5BF9;&#x5E94;&#x4E00;&#x4E2A;State&#x5B9E;&#x4F8B;&#x3002;</p>
<blockquote>
<p>&#x5728;&#x672C;&#x4E66;&#x4E2D;&#x7ECF;&#x5E38;&#x4F1A;&#x51FA;&#x73B0;&#x201C;&#x6811;&#x201D;&#x7684;&#x6982;&#x5FF5;&#xFF0C;&#x5728;&#x4E0D;&#x540C;&#x7684;&#x573A;&#x666F;&#x53EF;&#x80FD;&#x6307;&#x4E0D;&#x540C;&#x7684;&#x610F;&#x601D;&#xFF0C;&#x5728;&#x8BF4;&#x201C;widget&#x6811;&#x201D;&#x65F6;&#x5B83;&#x53EF;&#x4EE5;&#x6307;widget&#x7ED3;&#x6784;&#x6811;&#xFF0C;&#x4F46;&#x7531;&#x4E8E;widget&#x4E0E;Element&#x6709;&#x5BF9;&#x5E94;&#x5173;&#x7CFB;&#xFF08;&#x4E00;&#x53EF;&#x80FD;&#x5BF9;&#x591A;&#xFF09;&#xFF0C;&#x5728;&#x6709;&#x4E9B;&#x573A;&#x666F;&#xFF08;Flutter&#x7684;SDK&#x6587;&#x6863;&#x4E2D;&#xFF09;&#x4E5F;&#x4EE3;&#x6307;&#x201C;UI&#x6811;&#x201D;&#x7684;&#x610F;&#x601D;&#x3002;&#x800C;&#x5728;stateful widget&#x4E2D;&#xFF0C;State&#x5BF9;&#x8C61;&#x4E5F;&#x548C;<code>StatefulElement</code>&#x5177;&#x6709;&#x5BF9;&#x5E94;&#x5173;&#x7CFB;&#xFF08;&#x4E00;&#x5BF9;&#x4E00;&#xFF09;&#xFF0C;&#x6240;&#x4EE5;&#x5728;Flutter&#x7684;SDK&#x6587;&#x6863;&#x4E2D;&#xFF0C;&#x53EF;&#x4EE5;&#x7ECF;&#x5E38;&#x770B;&#x5230;&#x201C;&#x4ECE;&#x6811;&#x4E2D;&#x79FB;&#x9664;State&#x5BF9;&#x8C61;&#x201D;&#x6216;&#x201C;&#x63D2;&#x5165;State&#x5BF9;&#x8C61;&#x5230;&#x6811;&#x4E2D;&#x201D;&#x8FD9;&#x6837;&#x7684;&#x63CF;&#x8FF0;&#x3002;&#x5176;&#x5B9E;&#xFF0C;&#x65E0;&#x8BBA;&#x54EA;&#x79CD;&#x63CF;&#x8FF0;&#xFF0C;&#x5176;&#x610F;&#x601D;&#x90FD;&#x662F;&#x5728;&#x63CF;&#x8FF0;&#x201C;&#x4E00;&#x68F5;&#x6784;&#x6210;&#x7528;&#x6237;&#x754C;&#x9762;&#x7684;&#x8282;&#x70B9;&#x5143;&#x7D20;&#x7684;&#x6811;&#x201D;&#xFF0C;&#x8BFB;&#x8005;&#x4E0D;&#x5FC5;&#x7EA0;&#x7ED3;&#x4E8E;&#x8FD9;&#x4E9B;&#x6982;&#x5FF5;&#xFF0C;&#x8FD8;&#x662F;&#x90A3;&#x53E5;&#x8BDD;&#x201C;&#x5F97;&#x5176;&#x795E;&#xFF0C;&#x5FD8;&#x5176;&#x5F62;&#x201D;&#xFF0C;&#x56E0;&#x6B64;&#xFF0C;&#x672C;&#x4E66;&#x4E2D;&#x51FA;&#x73B0;&#x7684;&#x5404;&#x79CD;&#x201C;&#x6811;&#x201D;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x7279;&#x522B;&#x8BF4;&#x660E;&#xFF0C;&#x8BFB;&#x8005;&#x90FD;&#x53EF;&#x62BD;&#x8C61;&#x7684;&#x8BA4;&#x4E3A;&#x5B83;&#x662F;&#x201C;&#x4E00;&#x68F5;&#x6784;&#x6210;&#x7528;&#x6237;&#x754C;&#x9762;&#x7684;&#x8282;&#x70B9;&#x5143;&#x7D20;&#x7684;&#x6811;&#x201D;&#x3002;</p>
</blockquote>
</li>
</ul>
<h2 id="316-state">3.1.6 State</h2>
<p>&#x4E00;&#x4E2A;StatefulWidget&#x7C7B;&#x4F1A;&#x5BF9;&#x5E94;&#x4E00;&#x4E2A;State&#x7C7B;&#xFF0C;State&#x8868;&#x793A;&#x4E0E;&#x5176;&#x5BF9;&#x5E94;&#x7684;StatefulWidget&#x8981;&#x7EF4;&#x62A4;&#x7684;&#x72B6;&#x6001;&#xFF0C;State&#x4E2D;&#x7684;&#x4FDD;&#x5B58;&#x7684;&#x72B6;&#x6001;&#x4FE1;&#x606F;&#x53EF;&#x4EE5;&#xFF1A;</p>
<ol>
<li>&#x5728;widget &#x6784;&#x5EFA;&#x65F6;&#x53EF;&#x4EE5;&#x88AB;&#x540C;&#x6B65;&#x8BFB;&#x53D6;&#x3002;</li>
<li>&#x5728;widget&#x751F;&#x547D;&#x5468;&#x671F;&#x4E2D;&#x53EF;&#x4EE5;&#x88AB;&#x6539;&#x53D8;&#xFF0C;&#x5F53;State&#x88AB;&#x6539;&#x53D8;&#x65F6;&#xFF0C;&#x53EF;&#x4EE5;&#x624B;&#x52A8;&#x8C03;&#x7528;&#x5176;<code>setState()</code>&#x65B9;&#x6CD5;&#x901A;&#x77E5;Flutter framework&#x72B6;&#x6001;&#x53D1;&#x751F;&#x6539;&#x53D8;&#xFF0C;Flutter framework&#x5728;&#x6536;&#x5230;&#x6D88;&#x606F;&#x540E;&#xFF0C;&#x4F1A;&#x91CD;&#x65B0;&#x8C03;&#x7528;&#x5176;<code>build</code>&#x65B9;&#x6CD5;&#x91CD;&#x65B0;&#x6784;&#x5EFA;widget&#x6811;&#xFF0C;&#x4ECE;&#x800C;&#x8FBE;&#x5230;&#x66F4;&#x65B0;UI&#x7684;&#x76EE;&#x7684;&#x3002;</li>
</ol>
<p>State&#x4E2D;&#x6709;&#x4E24;&#x4E2A;&#x5E38;&#x7528;&#x5C5E;&#x6027;&#xFF1A;</p>
<ol>
<li><p><code>widget</code>&#xFF0C;&#x5B83;&#x8868;&#x793A;&#x4E0E;&#x8BE5;State&#x5B9E;&#x4F8B;&#x5173;&#x8054;&#x7684;widget&#x5B9E;&#x4F8B;&#xFF0C;&#x7531;Flutter framework&#x52A8;&#x6001;&#x8BBE;&#x7F6E;&#x3002;&#x6CE8;&#x610F;&#xFF0C;&#x8FD9;&#x79CD;&#x5173;&#x8054;&#x5E76;&#x975E;&#x6C38;&#x4E45;&#x7684;&#xFF0C;&#x56E0;&#x4E3A;&#x5728;&#x5E94;&#x7528;&#x751F;&#x547D;&#x5468;&#x671F;&#x4E2D;&#xFF0C;UI&#x6811;&#x4E0A;&#x7684;&#x67D0;&#x4E00;&#x4E2A;&#x8282;&#x70B9;&#x7684;widget&#x5B9E;&#x4F8B;&#x5728;&#x91CD;&#x65B0;&#x6784;&#x5EFA;&#x65F6;&#x53EF;&#x80FD;&#x4F1A;&#x53D8;&#x5316;&#xFF0C;&#x4F46;State&#x5B9E;&#x4F8B;&#x53EA;&#x4F1A;&#x5728;&#x7B2C;&#x4E00;&#x6B21;&#x63D2;&#x5165;&#x5230;&#x6811;&#x4E2D;&#x65F6;&#x88AB;&#x521B;&#x5EFA;&#xFF0C;&#x5F53;&#x5728;&#x91CD;&#x65B0;&#x6784;&#x5EFA;&#x65F6;&#xFF0C;&#x5982;&#x679C;widget&#x88AB;&#x4FEE;&#x6539;&#x4E86;&#xFF0C;Flutter framework&#x4F1A;&#x52A8;&#x6001;&#x8BBE;&#x7F6E;State.widget&#x4E3A;&#x65B0;&#x7684;widget&#x5B9E;&#x4F8B;&#x3002;</p>
</li>
<li><p><code>context</code>&#x3002;StatefulWidget&#x5BF9;&#x5E94;&#x7684;BuildContext&#xFF0C;&#x4F5C;&#x7528;&#x540C;StatelessWidget&#x7684;BuildContext&#x3002;</p>
</li>
</ol>
<h4 id="state&#x751F;&#x547D;&#x5468;&#x671F;">State&#x751F;&#x547D;&#x5468;&#x671F;</h4>
<p>&#x7406;&#x89E3;State&#x7684;&#x751F;&#x547D;&#x5468;&#x671F;&#x5BF9;flutter&#x5F00;&#x53D1;&#x975E;&#x5E38;&#x91CD;&#x8981;&#xFF0C;&#x4E3A;&#x4E86;&#x52A0;&#x6DF1;&#x8BFB;&#x8005;&#x5370;&#x8C61;&#xFF0C;&#x672C;&#x8282;&#x6211;&#x4EEC;&#x901A;&#x8FC7;&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;&#x6765;&#x6F14;&#x793A;&#x4E00;&#x4E0B;State&#x7684;&#x751F;&#x547D;&#x5468;&#x671F;&#x3002;&#x5728;&#x63A5;&#x4E0B;&#x6765;&#x7684;&#x793A;&#x4F8B;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x8BA1;&#x6570;&#x5668;widget&#xFF0C;&#x70B9;&#x51FB;&#x5B83;&#x53EF;&#x4EE5;&#x4F7F;&#x8BA1;&#x6570;&#x5668;&#x52A0;1&#xFF0C;&#x7531;&#x4E8E;&#x8981;&#x4FDD;&#x5B58;&#x8BA1;&#x6570;&#x5668;&#x7684;&#x6570;&#x503C;&#x72B6;&#x6001;&#xFF0C;&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x5E94;&#x7EE7;&#x627F;StatefulWidget&#xFF0C;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"><span class="token keyword">class</span> <span class="token class-name">CounterWidget</span> <span class="token keyword">extends</span> <span class="token class-name">StatefulWidget</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> <span class="token function">CounterWidget</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
    Key key<span class="token punctuation">,</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>initValue<span class="token punctuation">:</span> <span class="token number">0</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword">final</span> int initValue<span class="token punctuation">;</span>

  <span class="token metadata symbol">@override</span>
  _CounterWidgetState <span class="token function">createState</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token keyword">new</span> <span class="token class-name">_CounterWidgetState</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p><code>CounterWidget</code>&#x63A5;&#x6536;&#x4E00;&#x4E2A;<code>initValue</code>&#x6574;&#x578B;&#x53C2;&#x6570;&#xFF0C;&#x5B83;&#x8868;&#x793A;&#x8BA1;&#x6570;&#x5668;&#x7684;&#x521D;&#x59CB;&#x503C;&#x3002;&#x4E0B;&#x9762;&#x6211;&#x4EEC;&#x770B;&#x4E00;&#x4E0B;State&#x7684;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"><span class="token keyword">class</span> <span class="token class-name">_CounterWidgetState</span> <span class="token keyword">extends</span> <span class="token class-name">State</span><span class="token operator">&lt;</span>CounterWidget<span class="token operator">&gt;</span> <span class="token punctuation">{</span>  
  int _counter<span class="token punctuation">;</span>

  <span class="token metadata symbol">@override</span>
  <span class="token keyword">void</span> <span class="token function">initState</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">initState</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//&#x521D;&#x59CB;&#x5316;&#x72B6;&#x6001;  </span>
    _counter<span class="token operator">=</span>widget<span class="token punctuation">.</span>initValue<span class="token punctuation">;</span>
    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">&quot;initState&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token metadata symbol">@override</span>
  Widget <span class="token function">build</span><span class="token punctuation">(</span>BuildContext context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">&quot;build&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token function">Scaffold</span><span class="token punctuation">(</span>
      body<span class="token punctuation">:</span> <span class="token function">Center</span><span class="token punctuation">(</span>
        child<span class="token punctuation">:</span> <span class="token function">FlatButton</span><span class="token punctuation">(</span>
          child<span class="token punctuation">:</span> <span class="token function">Text</span><span class="token punctuation">(</span><span class="token string">&apos;$_counter&apos;</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
          <span class="token comment">//&#x70B9;&#x51FB;&#x540E;&#x8BA1;&#x6570;&#x5668;&#x81EA;&#x589E;</span>
          onPressed<span class="token punctuation">:</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token operator">&gt;</span><span class="token function">setState</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token operator">++</span>_counter<span class="token punctuation">,</span>
          <span class="token punctuation">)</span><span class="token punctuation">,</span>
        <span class="token punctuation">)</span><span class="token punctuation">,</span>
      <span class="token punctuation">)</span><span class="token punctuation">,</span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token metadata symbol">@override</span>
  <span class="token keyword">void</span> <span class="token function">didUpdateWidget</span><span class="token punctuation">(</span>CounterWidget oldWidget<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">didUpdateWidget</span><span class="token punctuation">(</span>oldWidget<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">&quot;didUpdateWidget&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token metadata symbol">@override</span>
  <span class="token keyword">void</span> <span class="token function">deactivate</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">deactivate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">&quot;deactive&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token metadata symbol">@override</span>
  <span class="token keyword">void</span> <span class="token function">dispose</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">dispose</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">&quot;dispose&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token metadata symbol">@override</span>
  <span class="token keyword">void</span> <span class="token function">reassemble</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">reassemble</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">&quot;reassemble&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token metadata symbol">@override</span>
  <span class="token keyword">void</span> <span class="token function">didChangeDependencies</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">didChangeDependencies</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">&quot;didChangeDependencies&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

<span class="token punctuation">}</span>
</code></pre>
<p>&#x63A5;&#x4E0B;&#x6765;&#xFF0C;&#x6211;&#x4EEC;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x8DEF;&#x7531;&#xFF0C;&#x5728;&#x65B0;&#x8DEF;&#x7531;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x53EA;&#x663E;&#x793A;&#x4E00;&#x4E2A;<code>CounterWidget</code>&#xFF1A;</p>
<pre class="language-"><code class="lang-dart">Widget <span class="token function">build</span><span class="token punctuation">(</span>BuildContext context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token function">CounterWidget</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x8FD0;&#x884C;&#x5E94;&#x7528;&#x5E76;&#x6253;&#x5F00;&#x8BE5;&#x8DEF;&#x7531;&#x9875;&#x9762;&#xFF0C;&#x5728;&#x65B0;&#x8DEF;&#x7531;&#x9875;&#x6253;&#x5F00;&#x540E;&#xFF0C;&#x5C4F;&#x5E55;&#x4E2D;&#x592E;&#x5C31;&#x4F1A;&#x51FA;&#x73B0;&#x4E00;&#x4E2A;&#x6570;&#x5B57;0&#xFF0C;&#x7136;&#x540E;&#x63A7;&#x5236;&#x53F0;&#x65E5;&#x5FD7;&#x8F93;&#x51FA;&#xFF1A;</p>
<pre class="language-"><code>I/flutter ( 5436): initState
I/flutter ( 5436): didChangeDependencies
I/flutter ( 5436): build
</code></pre><p>&#x53EF;&#x4EE5;&#x770B;&#x5230;&#xFF0C;&#x5728;StatefulWidget&#x63D2;&#x5165;&#x5230;Widget&#x6811;&#x65F6;&#x9996;&#x5148;<code>initState</code>&#x65B9;&#x6CD5;&#x4F1A;&#x88AB;&#x8C03;&#x7528;&#x3002;</p>
<p>&#x7136;&#x540E;&#x6211;&#x4EEC;&#x70B9;&#x51FB;&#x26A1;&#xFE0F;&#x6309;&#x94AE;&#x70ED;&#x91CD;&#x8F7D;&#xFF0C;&#x63A7;&#x5236;&#x53F0;&#x8F93;&#x51FA;&#x65E5;&#x5FD7;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre class="language-"><code>I/flutter ( 5436): reassemble
I/flutter ( 5436): didUpdateWidget
I/flutter ( 5436): build
</code></pre><p>&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x6B64;&#x65F6;<code>initState</code> &#x548C;<code>didChangeDependencies</code>&#x90FD;&#x6CA1;&#x6709;&#x88AB;&#x8C03;&#x7528;&#xFF0C;&#x800C;&#x6B64;&#x65F6;<code>didUpdateWidget</code>&#x88AB;&#x8C03;&#x7528;&#x3002;</p>
<p>&#x63A5;&#x4E0B;&#x6765;&#xFF0C;&#x6211;&#x4EEC;&#x5728;widget&#x6811;&#x4E2D;&#x79FB;&#x9664;<code>CounterWidget</code>&#xFF0C;&#x5C06;&#x8DEF;&#x7531;<code>build</code>&#x65B9;&#x6CD5;&#x6539;&#x4E3A;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart">Widget <span class="token function">build</span><span class="token punctuation">(</span>BuildContext context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">//&#x79FB;&#x9664;&#x8BA1;&#x6570;&#x5668; </span>
  <span class="token comment">//return CounterWidget();</span>
  <span class="token comment">//&#x968F;&#x4FBF;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;Text()</span>
  <span class="token keyword">return</span> <span class="token function">Text</span><span class="token punctuation">(</span><span class="token string">&quot;xxx&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x7136;&#x540E;&#x70ED;&#x91CD;&#x8F7D;&#xFF0C;&#x65E5;&#x5FD7;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre class="language-"><code>I/flutter ( 5436): reassemble
I/flutter ( 5436): deactive
I/flutter ( 5436): dispose
</code></pre><p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#xFF0C;&#x5728;<code>CounterWidget</code>&#x4ECE;widget&#x6811;&#x4E2D;&#x79FB;&#x9664;&#x65F6;&#xFF0C;<code>deactive</code>&#x548C;<code>dispose</code>&#x4F1A;&#x4F9D;&#x6B21;&#x88AB;&#x8C03;&#x7528;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x6211;&#x4EEC;&#x6765;&#x770B;&#x770B;&#x5404;&#x4E2A;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#xFF1A;</p>
<ul>
<li><code>initState</code>&#xFF1A;&#x5F53;Widget&#x7B2C;&#x4E00;&#x6B21;&#x63D2;&#x5165;&#x5230;Widget&#x6811;&#x65F6;&#x4F1A;&#x88AB;&#x8C03;&#x7528;&#xFF0C;&#x5BF9;&#x4E8E;&#x6BCF;&#x4E00;&#x4E2A;State&#x5BF9;&#x8C61;&#xFF0C;Flutter framework&#x53EA;&#x4F1A;&#x8C03;&#x7528;&#x4E00;&#x6B21;&#x8BE5;&#x56DE;&#x8C03;&#xFF0C;&#x6240;&#x4EE5;&#xFF0C;&#x901A;&#x5E38;&#x5728;&#x8BE5;&#x56DE;&#x8C03;&#x4E2D;&#x505A;&#x4E00;&#x4E9B;&#x4E00;&#x6B21;&#x6027;&#x7684;&#x64CD;&#x4F5C;&#xFF0C;&#x5982;&#x72B6;&#x6001;&#x521D;&#x59CB;&#x5316;&#x3001;&#x8BA2;&#x9605;&#x5B50;&#x6811;&#x7684;&#x4E8B;&#x4EF6;&#x901A;&#x77E5;&#x7B49;&#x3002;&#x4E0D;&#x80FD;&#x5728;&#x8BE5;&#x56DE;&#x8C03;&#x4E2D;&#x8C03;&#x7528;<code>BuildContext.inheritFromWidgetOfExactType</code>&#xFF08;&#x8BE5;&#x65B9;&#x6CD5;&#x7528;&#x4E8E;&#x5728;Widget&#x6811;&#x4E0A;&#x83B7;&#x53D6;&#x79BB;&#x5F53;&#x524D;widget&#x6700;&#x8FD1;&#x7684;&#x4E00;&#x4E2A;&#x7236;&#x7EA7;<code>InheritFromWidget</code>&#xFF0C;&#x5173;&#x4E8E;<code>InheritedWidget</code>&#x6211;&#x4EEC;&#x5C06;&#x5728;&#x540E;&#x9762;&#x7AE0;&#x8282;&#x4ECB;&#x7ECD;&#xFF09;&#xFF0C;&#x539F;&#x56E0;&#x662F;&#x5728;&#x521D;&#x59CB;&#x5316;&#x5B8C;&#x6210;&#x540E;&#xFF0C;Widget&#x6811;&#x4E2D;&#x7684;<code>InheritFromWidget</code>&#x4E5F;&#x53EF;&#x80FD;&#x4F1A;&#x53D1;&#x751F;&#x53D8;&#x5316;&#xFF0C;&#x6240;&#x4EE5;&#x6B63;&#x786E;&#x7684;&#x505A;&#x6CD5;&#x5E94;&#x8BE5;&#x5728;&#x5728;<code>build&#xFF08;&#xFF09;</code>&#x65B9;&#x6CD5;&#x6216;<code>didChangeDependencies()</code>&#x4E2D;&#x8C03;&#x7528;&#x5B83;&#x3002;</li>
<li><code>didChangeDependencies()</code>&#xFF1A;&#x5F53;State&#x5BF9;&#x8C61;&#x7684;&#x4F9D;&#x8D56;&#x53D1;&#x751F;&#x53D8;&#x5316;&#x65F6;&#x4F1A;&#x88AB;&#x8C03;&#x7528;&#xFF1B;&#x4F8B;&#x5982;&#xFF1A;&#x5728;&#x4E4B;&#x524D;<code>build()</code> &#x4E2D;&#x5305;&#x542B;&#x4E86;&#x4E00;&#x4E2A;<code>InheritedWidget</code>&#xFF0C;&#x7136;&#x540E;&#x5728;&#x4E4B;&#x540E;&#x7684;<code>build()</code> &#x4E2D;<code>InheritedWidget</code>&#x53D1;&#x751F;&#x4E86;&#x53D8;&#x5316;&#xFF0C;&#x90A3;&#x4E48;&#x6B64;&#x65F6;<code>InheritedWidget</code>&#x7684;&#x5B50;widget&#x7684;<code>didChangeDependencies()</code>&#x56DE;&#x8C03;&#x90FD;&#x4F1A;&#x88AB;&#x8C03;&#x7528;&#x3002;&#x5178;&#x578B;&#x7684;&#x573A;&#x666F;&#x662F;&#x5F53;&#x7CFB;&#x7EDF;&#x8BED;&#x8A00;Locale&#x6216;&#x5E94;&#x7528;&#x4E3B;&#x9898;&#x6539;&#x53D8;&#x65F6;&#xFF0C;Flutter framework&#x4F1A;&#x901A;&#x77E5;widget&#x8C03;&#x7528;&#x6B64;&#x56DE;&#x8C03;&#x3002;</li>
<li><p><code>build()</code>&#xFF1A;&#x6B64;&#x56DE;&#x8C03;&#x8BFB;&#x8005;&#x73B0;&#x5728;&#x5E94;&#x8BE5;&#x5DF2;&#x7ECF;&#x76F8;&#x5F53;&#x719F;&#x6089;&#x4E86;&#xFF0C;&#x5B83;&#x4E3B;&#x8981;&#x662F;&#x7528;&#x4E8E;&#x6784;&#x5EFA;Widget&#x5B50;&#x6811;&#x7684;&#xFF0C;&#x4F1A;&#x5728;&#x5982;&#x4E0B;&#x573A;&#x666F;&#x88AB;&#x8C03;&#x7528;&#xFF1A;</p>
<ol>
<li>&#x5728;&#x8C03;&#x7528;<code>initState()</code>&#x4E4B;&#x540E;&#x3002;</li>
<li>&#x5728;&#x8C03;&#x7528;<code>didUpdateWidget()</code>&#x4E4B;&#x540E;&#x3002;</li>
<li>&#x5728;&#x8C03;&#x7528;<code>setState()</code>&#x4E4B;&#x540E;&#x3002;</li>
<li>&#x5728;&#x8C03;&#x7528;<code>didChangeDependencies()</code>&#x4E4B;&#x540E;&#x3002;</li>
<li>&#x5728;State&#x5BF9;&#x8C61;&#x4ECE;&#x6811;&#x4E2D;&#x4E00;&#x4E2A;&#x4F4D;&#x7F6E;&#x79FB;&#x9664;&#x540E;&#xFF08;&#x4F1A;&#x8C03;&#x7528;deactivate&#xFF09;&#x53C8;&#x91CD;&#x65B0;&#x63D2;&#x5165;&#x5230;&#x6811;&#x7684;&#x5176;&#x5B83;&#x4F4D;&#x7F6E;&#x4E4B;&#x540E;&#x3002;</li>
</ol>
</li>
<li><code>reassemble()</code>&#xFF1A;&#x6B64;&#x56DE;&#x8C03;&#x662F;&#x4E13;&#x95E8;&#x4E3A;&#x4E86;&#x5F00;&#x53D1;&#x8C03;&#x8BD5;&#x800C;&#x63D0;&#x4F9B;&#x7684;&#xFF0C;&#x5728;&#x70ED;&#x91CD;&#x8F7D;(hot reload)&#x65F6;&#x4F1A;&#x88AB;&#x8C03;&#x7528;&#xFF0C;&#x6B64;&#x56DE;&#x8C03;&#x5728;Release&#x6A21;&#x5F0F;&#x4E0B;&#x6C38;&#x8FDC;&#x4E0D;&#x4F1A;&#x88AB;&#x8C03;&#x7528;&#x3002;</li>
<li><code>didUpdateWidget()</code>&#xFF1A;&#x5728;widget&#x91CD;&#x65B0;&#x6784;&#x5EFA;&#x65F6;&#xFF0C;Flutter framework&#x4F1A;&#x8C03;&#x7528;<code>Widget.canUpdate</code>&#x6765;&#x68C0;&#x6D4B;Widget&#x6811;&#x4E2D;&#x540C;&#x4E00;&#x4F4D;&#x7F6E;&#x7684;&#x65B0;&#x65E7;&#x8282;&#x70B9;&#xFF0C;&#x7136;&#x540E;&#x51B3;&#x5B9A;&#x662F;&#x5426;&#x9700;&#x8981;&#x66F4;&#x65B0;&#xFF0C;&#x5982;&#x679C;<code>Widget.canUpdate</code>&#x8FD4;&#x56DE;<code>true</code>&#x5219;&#x4F1A;&#x8C03;&#x7528;&#x6B64;&#x56DE;&#x8C03;&#x3002;&#x6B63;&#x5982;&#x4E4B;&#x524D;&#x6240;&#x8FF0;&#xFF0C;<code>Widget.canUpdate</code>&#x4F1A;&#x5728;&#x65B0;&#x65E7;widget&#x7684;key&#x548C;runtimeType&#x540C;&#x65F6;&#x76F8;&#x7B49;&#x65F6;&#x4F1A;&#x8FD4;&#x56DE;true&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#x5728;&#x5728;&#x65B0;&#x65E7;widget&#x7684;key&#x548C;runtimeType&#x540C;&#x65F6;&#x76F8;&#x7B49;&#x65F6;<code>didUpdateWidget()</code>&#x5C31;&#x4F1A;&#x88AB;&#x8C03;&#x7528;&#x3002;</li>
<li><code>deactivate()</code>&#xFF1A;&#x5F53;State&#x5BF9;&#x8C61;&#x4ECE;&#x6811;&#x4E2D;&#x88AB;&#x79FB;&#x9664;&#x65F6;&#xFF0C;&#x4F1A;&#x8C03;&#x7528;&#x6B64;&#x56DE;&#x8C03;&#x3002;&#x5728;&#x4E00;&#x4E9B;&#x573A;&#x666F;&#x4E0B;&#xFF0C;Flutter framework&#x4F1A;&#x5C06;State&#x5BF9;&#x8C61;&#x91CD;&#x65B0;&#x63D2;&#x5230;&#x6811;&#x4E2D;&#xFF0C;&#x5982;&#x5305;&#x542B;&#x6B64;State&#x5BF9;&#x8C61;&#x7684;&#x5B50;&#x6811;&#x5728;&#x6811;&#x7684;&#x4E00;&#x4E2A;&#x4F4D;&#x7F6E;&#x79FB;&#x52A8;&#x5230;&#x53E6;&#x4E00;&#x4E2A;&#x4F4D;&#x7F6E;&#x65F6;&#xFF08;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;GlobalKey&#x6765;&#x5B9E;&#x73B0;&#xFF09;&#x3002;&#x5982;&#x679C;&#x79FB;&#x9664;&#x540E;&#x6CA1;&#x6709;&#x91CD;&#x65B0;&#x63D2;&#x5165;&#x5230;&#x6811;&#x4E2D;&#x5219;&#x7D27;&#x63A5;&#x7740;&#x4F1A;&#x8C03;&#x7528;<code>dispose()</code>&#x65B9;&#x6CD5;&#x3002;</li>
<li><code>dispose()</code>&#xFF1A;&#x5F53;State&#x5BF9;&#x8C61;&#x4ECE;&#x6811;&#x4E2D;&#x88AB;&#x6C38;&#x4E45;&#x79FB;&#x9664;&#x65F6;&#x8C03;&#x7528;&#xFF1B;&#x901A;&#x5E38;&#x5728;&#x6B64;&#x56DE;&#x8C03;&#x4E2D;&#x91CA;&#x653E;&#x8D44;&#x6E90;&#x3002;</li>
</ul>
<p>StatefulWidget&#x751F;&#x547D;&#x5468;&#x671F;&#x5982;&#x56FE;3-2&#x6240;&#x793A;&#xFF1A;</p>
<p><img src="https://pcdn.flutterchina.club/imgs/3-2.jpg" alt="&#x56FE;3-2"></p>
<blockquote>
<p><strong>&#x6CE8;&#x610F;</strong>&#xFF1A;&#x5728;&#x7EE7;&#x627F;<code>StatefulWidget</code>&#x91CD;&#x5199;&#x5176;&#x65B9;&#x6CD5;&#x65F6;&#xFF0C;&#x5BF9;&#x4E8E;&#x5305;&#x542B;<code>@mustCallSuper</code>&#x6807;&#x6CE8;&#x7684;&#x7236;&#x7C7B;&#x65B9;&#x6CD5;&#xFF0C;&#x90FD;&#x8981;&#x5728;&#x5B50;&#x7C7B;&#x65B9;&#x6CD5;&#x4E2D;&#x5148;&#x8C03;&#x7528;&#x7236;&#x7C7B;&#x65B9;&#x6CD5;&#x3002;</p>
</blockquote>
<h3 id="&#x4E3A;&#x4EC0;&#x4E48;&#x8981;&#x5C06;build&#x65B9;&#x6CD5;&#x653E;&#x5728;state&#x4E2D;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x653E;&#x5728;statefulwidget&#x4E2D;&#xFF1F;">&#x4E3A;&#x4EC0;&#x4E48;&#x8981;&#x5C06;build&#x65B9;&#x6CD5;&#x653E;&#x5728;State&#x4E2D;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x653E;&#x5728;StatefulWidget&#x4E2D;&#xFF1F;</h3>
<p>&#x73B0;&#x5728;&#xFF0C;&#x6211;&#x4EEC;&#x56DE;&#x7B54;&#x4E4B;&#x524D;&#x63D0;&#x51FA;&#x7684;&#x95EE;&#x9898;&#xFF0C;&#x4E3A;&#x4EC0;&#x4E48;<code>build()</code>&#x65B9;&#x6CD5;&#x653E;&#x5728;State&#xFF08;&#x800C;&#x4E0D;&#x662F;<code>StatefulWidget</code>&#xFF09;&#x4E2D; &#xFF1F;&#x8FD9;&#x4E3B;&#x8981;&#x662F;&#x4E3A;&#x4E86;&#x63D0;&#x9AD8;&#x5F00;&#x53D1;&#x7684;&#x7075;&#x6D3B;&#x6027;&#x3002;&#x5982;&#x679C;&#x5C06;<code>build()</code>&#x65B9;&#x6CD5;&#x5728;<code>StatefulWidget</code>&#x4E2D;&#x5219;&#x4F1A;&#x6709;&#x4E24;&#x4E2A;&#x95EE;&#x9898;&#xFF1A;</p>
<ul>
<li><p>&#x72B6;&#x6001;&#x8BBF;&#x95EE;&#x4E0D;&#x4FBF;&#x3002;</p>
<p>&#x8BD5;&#x60F3;&#x4E00;&#x4E0B;&#xFF0C;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x7684;<code>StatefulWidget</code>&#x6709;&#x5F88;&#x591A;&#x72B6;&#x6001;&#xFF0C;&#x800C;&#x6BCF;&#x6B21;&#x72B6;&#x6001;&#x6539;&#x53D8;&#x90FD;&#x8981;&#x8C03;&#x7528;<code>build</code>&#x65B9;&#x6CD5;&#xFF0C;&#x7531;&#x4E8E;&#x72B6;&#x6001;&#x662F;&#x4FDD;&#x5B58;&#x5728;State&#x4E2D;&#x7684;&#xFF0C;&#x5982;&#x679C;<code>build</code>&#x65B9;&#x6CD5;&#x5728;<code>StatefulWidget</code>&#x4E2D;&#xFF0C;&#x90A3;&#x4E48;<code>build</code>&#x65B9;&#x6CD5;&#x548C;&#x72B6;&#x6001;&#x5206;&#x522B;&#x5728;&#x4E24;&#x4E2A;&#x7C7B;&#x4E2D;&#xFF0C;&#x90A3;&#x4E48;&#x6784;&#x5EFA;&#x65F6;&#x8BFB;&#x53D6;&#x72B6;&#x6001;&#x5C06;&#x4F1A;&#x5F88;&#x4E0D;&#x65B9;&#x4FBF;&#xFF01;&#x8BD5;&#x60F3;&#x4E00;&#x4E0B;&#xFF0C;&#x5982;&#x679C;&#x771F;&#x7684;&#x5C06;<code>build</code>&#x65B9;&#x6CD5;&#x653E;&#x5728;StatefulWidget&#x4E2D;&#x7684;&#x8BDD;&#xFF0C;&#x7531;&#x4E8E;&#x6784;&#x5EFA;&#x7528;&#x6237;&#x754C;&#x9762;&#x8FC7;&#x7A0B;&#x9700;&#x8981;&#x4F9D;&#x8D56;State&#xFF0C;&#x6240;&#x4EE5;<code>build</code>&#x65B9;&#x6CD5;&#x5C06;&#x5FC5;&#x987B;&#x52A0;&#x4E00;&#x4E2A;<code>State</code>&#x53C2;&#x6570;&#xFF0C;&#x5927;&#x6982;&#x662F;&#x4E0B;&#x9762;&#x8FD9;&#x6837;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart">  Widget <span class="token function">build</span><span class="token punctuation">(</span>BuildContext context<span class="token punctuation">,</span> State state<span class="token punctuation">)</span><span class="token punctuation">{</span>
      <span class="token comment">//state.counter</span>
      <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
  <span class="token punctuation">}</span>
</code></pre>
<p>&#x8FD9;&#x6837;&#x7684;&#x8BDD;&#x5C31;&#x53EA;&#x80FD;&#x5C06;State&#x7684;&#x6240;&#x6709;&#x72B6;&#x6001;&#x58F0;&#x660E;&#x4E3A;&#x516C;&#x5F00;&#x7684;&#x72B6;&#x6001;&#xFF0C;&#x8FD9;&#x6837;&#x624D;&#x80FD;&#x5728;State&#x7C7B;&#x5916;&#x90E8;&#x8BBF;&#x95EE;&#x72B6;&#x6001;&#xFF01;&#x4F46;&#x662F;&#xFF0C;&#x5C06;&#x72B6;&#x6001;&#x8BBE;&#x7F6E;&#x4E3A;&#x516C;&#x5F00;&#x540E;&#xFF0C;&#x72B6;&#x6001;&#x5C06;&#x4E0D;&#x518D;&#x5177;&#x6709;&#x79C1;&#x5BC6;&#x6027;&#xFF0C;&#x8FD9;&#x5C31;&#x4F1A;&#x5BFC;&#x81F4;&#x5BF9;&#x72B6;&#x6001;&#x7684;&#x4FEE;&#x6539;&#x5C06;&#x4F1A;&#x53D8;&#x7684;&#x4E0D;&#x53EF;&#x63A7;&#x3002;&#x4F46;&#x5982;&#x679C;&#x5C06;<code>build()</code>&#x65B9;&#x6CD5;&#x653E;&#x5728;State&#x4E2D;&#x7684;&#x8BDD;&#xFF0C;&#x6784;&#x5EFA;&#x8FC7;&#x7A0B;&#x4E0D;&#x4EC5;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x8BBF;&#x95EE;&#x72B6;&#x6001;&#xFF0C;&#x800C;&#x4E14;&#x4E5F;&#x65E0;&#x9700;&#x516C;&#x5F00;&#x79C1;&#x6709;&#x72B6;&#x6001;&#xFF0C;&#x8FD9;&#x4F1A;&#x975E;&#x5E38;&#x65B9;&#x4FBF;&#x3002;</p>
</li>
<li><p>&#x7EE7;&#x627F;<code>StatefulWidget</code>&#x4E0D;&#x4FBF;&#x3002;</p>
<p>&#x4F8B;&#x5982;&#xFF0C;Flutter&#x4E2D;&#x6709;&#x4E00;&#x4E2A;&#x52A8;&#x753B;widget&#x7684;&#x57FA;&#x7C7B;<code>AnimatedWidget</code>&#xFF0C;&#x5B83;&#x7EE7;&#x627F;&#x81EA;<code>StatefulWidget</code>&#x7C7B;&#x3002;<code>AnimatedWidget</code>&#x4E2D;&#x5F15;&#x5165;&#x4E86;&#x4E00;&#x4E2A;&#x62BD;&#x8C61;&#x65B9;&#x6CD5;<code>build(BuildContext context)</code>&#xFF0C;&#x7EE7;&#x627F;&#x81EA;<code>AnimatedWidget</code>&#x7684;&#x52A8;&#x753B;widget&#x90FD;&#x8981;&#x5B9E;&#x73B0;&#x8FD9;&#x4E2A;<code>build</code>&#x65B9;&#x6CD5;&#x3002;&#x73B0;&#x5728;&#x8BBE;&#x60F3;&#x4E00;&#x4E0B;&#xFF0C;&#x5982;&#x679C;<code>StatefulWidget</code> &#x7C7B;&#x4E2D;&#x5DF2;&#x7ECF;&#x6709;&#x4E86;&#x4E00;&#x4E2A;<code>build</code>&#x65B9;&#x6CD5;&#xFF0C;&#x6B63;&#x5982;&#x4E0A;&#x9762;&#x6240;&#x8FF0;&#xFF0C;&#x6B64;&#x65F6;<code>build</code>&#x65B9;&#x6CD5;&#x9700;&#x8981;&#x63A5;&#x6536;&#x4E00;&#x4E2A;state&#x5BF9;&#x8C61;&#xFF0C;&#x8FD9;&#x5C31;&#x610F;&#x5473;&#x7740;<code>AnimatedWidget</code>&#x5FC5;&#x987B;&#x5C06;&#x81EA;&#x5DF1;&#x7684;State&#x5BF9;&#x8C61;(&#x8BB0;&#x4E3A;_animatedWidgetState)&#x63D0;&#x4F9B;&#x7ED9;&#x5176;&#x5B50;&#x7C7B;&#xFF0C;&#x56E0;&#x4E3A;&#x5B50;&#x7C7B;&#x9700;&#x8981;&#x5728;&#x5176;<code>build</code>&#x65B9;&#x6CD5;&#x4E2D;&#x8C03;&#x7528;&#x7236;&#x7C7B;&#x7684;<code>build</code>&#x65B9;&#x6CD5;&#xFF0C;&#x4EE3;&#x7801;&#x53EF;&#x80FD;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"><span class="token keyword">class</span> <span class="token class-name">MyAnimationWidget</span> <span class="token keyword">extends</span> <span class="token class-name">AnimatedWidget</span><span class="token punctuation">{</span>
    <span class="token metadata symbol">@override</span>
    Widget <span class="token function">build</span><span class="token punctuation">(</span>BuildContext context<span class="token punctuation">,</span> State state<span class="token punctuation">)</span><span class="token punctuation">{</span>
      <span class="token comment">//&#x7531;&#x4E8E;&#x5B50;&#x7C7B;&#x8981;&#x7528;&#x5230;AnimatedWidget&#x7684;&#x72B6;&#x6001;&#x5BF9;&#x8C61;_animatedWidgetState&#xFF0C;</span>
      <span class="token comment">//&#x6240;&#x4EE5;AnimatedWidget&#x5FC5;&#x987B;&#x901A;&#x8FC7;&#x67D0;&#x79CD;&#x65B9;&#x5F0F;&#x5C06;&#x5176;&#x72B6;&#x6001;&#x5BF9;&#x8C61;_animatedWidgetState</span>
      <span class="token comment">//&#x66B4;&#x9732;&#x7ED9;&#x5176;&#x5B50;&#x7C7B;   </span>
      <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span>context<span class="token punctuation">,</span> _animatedWidgetState<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x8FD9;&#x6837;&#x5F88;&#x663E;&#x7136;&#x662F;&#x4E0D;&#x5408;&#x7406;&#x7684;&#xFF0C;&#x56E0;&#x4E3A;</p>
<ol>
<li><code>AnimatedWidget</code>&#x7684;&#x72B6;&#x6001;&#x5BF9;&#x8C61;&#x662F;<code>AnimatedWidget</code>&#x5185;&#x90E8;&#x5B9E;&#x73B0;&#x7EC6;&#x8282;&#xFF0C;&#x4E0D;&#x5E94;&#x8BE5;&#x66B4;&#x9732;&#x7ED9;&#x5916;&#x90E8;&#x3002;</li>
<li>&#x5982;&#x679C;&#x8981;&#x5C06;&#x7236;&#x7C7B;&#x72B6;&#x6001;&#x66B4;&#x9732;&#x7ED9;&#x5B50;&#x7C7B;&#xFF0C;&#x90A3;&#x4E48;&#x5FC5;&#x987B;&#x5F97;&#x6709;&#x4E00;&#x79CD;&#x4F20;&#x9012;&#x673A;&#x5236;&#xFF0C;&#x800C;&#x505A;&#x8FD9;&#x4E00;&#x5957;&#x4F20;&#x9012;&#x673A;&#x5236;&#x662F;&#x65E0;&#x610F;&#x4E49;&#x7684;&#xFF0C;&#x56E0;&#x4E3A;&#x7236;&#x5B50;&#x7C7B;&#x4E4B;&#x95F4;&#x72B6;&#x6001;&#x7684;&#x4F20;&#x9012;&#x548C;&#x5B50;&#x7C7B;&#x672C;&#x8EAB;&#x903B;&#x8F91;&#x662F;&#x65E0;&#x5173;&#x7684;&#x3002;</li>
</ol>
</li>
</ul>
<p>&#x7EFC;&#x4E0A;&#x6240;&#x8FF0;&#xFF0C;&#x53EF;&#x4EE5;&#x53D1;&#x73B0;&#xFF0C;&#x5BF9;&#x4E8E;<code>StatefulWidget</code>&#xFF0C;&#x5C06;<code>build</code>&#x65B9;&#x6CD5;&#x653E;&#x5728;State&#x4E2D;&#xFF0C;&#x53EF;&#x4EE5;&#x7ED9;&#x5F00;&#x53D1;&#x5E26;&#x6765;&#x5F88;&#x5927;&#x7684;&#x7075;&#x6D3B;&#x6027;&#x3002;</p>
<h2 id="317-&#x5728;widget&#x6811;&#x4E2D;&#x83B7;&#x53D6;state&#x5BF9;&#x8C61;">3.1.7 &#x5728;Widget&#x6811;&#x4E2D;&#x83B7;&#x53D6;State&#x5BF9;&#x8C61;</h2>
<p>&#x7531;&#x4E8E;StatefulWidget&#x7684;&#x7684;&#x5177;&#x4F53;&#x903B;&#x8F91;&#x90FD;&#x5728;&#x5176;State&#x4E2D;&#xFF0C;&#x6240;&#x4EE5;&#x5F88;&#x591A;&#x65F6;&#x5019;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x83B7;&#x53D6;StatefulWidget&#x5BF9;&#x5E94;&#x7684;State&#x5BF9;&#x8C61;&#x6765;&#x8C03;&#x7528;&#x4E00;&#x4E9B;&#x65B9;&#x6CD5;&#xFF0C;&#x6BD4;&#x5982;<code>Scaffold</code>&#x7EC4;&#x4EF6;&#x5BF9;&#x5E94;&#x7684;&#x72B6;&#x6001;&#x7C7B;<code>ScaffoldState</code>&#x4E2D;&#x5C31;&#x5B9A;&#x4E49;&#x4E86;&#x6253;&#x5F00;SnackBar(&#x8DEF;&#x7531;&#x9875;&#x5E95;&#x90E8;&#x63D0;&#x793A;&#x6761;)&#x7684;&#x65B9;&#x6CD5;&#x3002;&#x6211;&#x4EEC;&#x6709;&#x4E24;&#x79CD;&#x65B9;&#x6CD5;&#x5728;&#x5B50;widget&#x6811;&#x4E2D;&#x83B7;&#x53D6;&#x7236;&#x7EA7;StatefulWidget&#x7684;State&#x5BF9;&#x8C61;&#x3002;</p>
<h3 id="&#x901A;&#x8FC7;context&#x83B7;&#x53D6;">&#x901A;&#x8FC7;Context&#x83B7;&#x53D6;</h3>
<p><code>context</code>&#x5BF9;&#x8C61;&#x6709;&#x4E00;&#x4E2A;<code>ancestorStateOfType(TypeMatcher)</code>&#x65B9;&#x6CD5;&#xFF0C;&#x8BE5;&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x4ECE;&#x5F53;&#x524D;&#x8282;&#x70B9;&#x6CBF;&#x7740;widget&#x6811;&#x5411;&#x4E0A;&#x67E5;&#x627E;&#x6307;&#x5B9A;&#x7C7B;&#x578B;&#x7684;StatefulWidget&#x5BF9;&#x5E94;&#x7684;State&#x5BF9;&#x8C61;&#x3002;&#x4E0B;&#x9762;&#x662F;&#x5B9E;&#x73B0;&#x6253;&#x5F00;SnackBar&#x7684;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"><span class="token function">Scaffold</span><span class="token punctuation">(</span>
  appBar<span class="token punctuation">:</span> <span class="token function">AppBar</span><span class="token punctuation">(</span>
    title<span class="token punctuation">:</span> <span class="token function">Text</span><span class="token punctuation">(</span><span class="token string">&quot;&#x5B50;&#x6811;&#x4E2D;&#x83B7;&#x53D6;State&#x5BF9;&#x8C61;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  <span class="token punctuation">)</span><span class="token punctuation">,</span>
  body<span class="token punctuation">:</span> <span class="token function">Center</span><span class="token punctuation">(</span>
    child<span class="token punctuation">:</span> <span class="token function">Builder</span><span class="token punctuation">(</span>builder<span class="token punctuation">:</span> <span class="token punctuation">(</span>context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">return</span> <span class="token function">RaisedButton</span><span class="token punctuation">(</span>
        onPressed<span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
          <span class="token comment">// &#x67E5;&#x627E;&#x7236;&#x7EA7;&#x6700;&#x8FD1;&#x7684;Scaffold&#x5BF9;&#x5E94;&#x7684;ScaffoldState&#x5BF9;&#x8C61;</span>
          ScaffoldState _state <span class="token operator">=</span> context<span class="token punctuation">.</span><span class="token function">ancestorStateOfType</span><span class="token punctuation">(</span>
              TypeMatcher<span class="token operator">&lt;</span>ScaffoldState<span class="token operator">&gt;</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
          <span class="token comment">//&#x8C03;&#x7528;ScaffoldState&#x7684;showSnackBar&#x6765;&#x5F39;&#x51FA;SnackBar</span>
          _state<span class="token punctuation">.</span><span class="token function">showSnackBar</span><span class="token punctuation">(</span>
            <span class="token function">SnackBar</span><span class="token punctuation">(</span>
              content<span class="token punctuation">:</span> <span class="token function">Text</span><span class="token punctuation">(</span><span class="token string">&quot;&#x6211;&#x662F;SnackBar&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            <span class="token punctuation">)</span><span class="token punctuation">,</span>
          <span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
        child<span class="token punctuation">:</span> <span class="token function">Text</span><span class="token punctuation">(</span><span class="token string">&quot;&#x663E;&#x793A;SnackBar&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
      <span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  <span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<p>&#x4E0A;&#x9762;&#x793A;&#x4F8B;&#x8FD0;&#x884C;&#x540E;&#xFF0C;&#x70B9;&#x51FB;&#x201D;&#x663E;&#x793A;SnackBar&#x201C;&#xFF0C;&#x6548;&#x679C;&#x5982;&#x56FE;3-1-2&#x6240;&#x793A;&#xFF1A;</p>
<p><img src="https://pcdn.flutterchina.club/imgs/3-1-2.png" alt="&#x56FE;3-1-2"></p>
<p>&#x4E00;&#x822C;&#x6765;&#x8BF4;&#xFF0C;&#x5982;&#x679C;StatefulWidget&#x7684;&#x72B6;&#x6001;&#x662F;&#x79C1;&#x6709;&#x7684;&#xFF08;&#x4E0D;&#x5E94;&#x8BE5;&#x5411;&#x5916;&#x90E8;&#x66B4;&#x9732;&#xFF09;&#xFF0C;&#x90A3;&#x4E48;&#x6211;&#x4EEC;&#x4EE3;&#x7801;&#x4E2D;&#x5C31;&#x4E0D;&#x5E94;&#x8BE5;&#x53BB;&#x76F4;&#x63A5;&#x83B7;&#x53D6;&#x5176;State&#x5BF9;&#x8C61;&#xFF1B;&#x5982;&#x679C;StatefulWidget&#x7684;&#x72B6;&#x6001;&#x662F;&#x5E0C;&#x671B;&#x66B4;&#x9732;&#x51FA;&#x7684;&#xFF08;&#x901A;&#x5E38;&#x8FD8;&#x6709;&#x4E00;&#x4E9B;&#x7EC4;&#x4EF6;&#x7684;&#x64CD;&#x4F5C;&#x65B9;&#x6CD5;&#xFF09;&#xFF0C;&#x6211;&#x4EEC;&#x5219;&#x53EF;&#x4EE5;&#x53BB;&#x76F4;&#x63A5;&#x83B7;&#x53D6;&#x5176;State&#x5BF9;&#x8C61;&#x3002;&#x4F46;&#x662F;&#x901A;&#x8FC7;<code>context.ancestorStateOfType</code>&#x83B7;&#x53D6;StatefulWidget&#x7684;&#x72B6;&#x6001;&#x7684;&#x65B9;&#x6CD5;&#x662F;&#x901A;&#x7528;&#x7684;&#xFF0C;&#x6211;&#x4EEC;&#x5E76;&#x4E0D;&#x80FD;&#x5728;&#x8BED;&#x6CD5;&#x5C42;&#x9762;&#x6307;&#x5B9A;StatefulWidget&#x7684;&#x72B6;&#x6001;&#x662F;&#x5426;&#x79C1;&#x6709;&#xFF0C;&#x6240;&#x4EE5;&#x5728;Flutter&#x5F00;&#x53D1;&#x4E2D;&#x4FBF;&#x6709;&#x4E86;&#x4E00;&#x4E2A;&#x9ED8;&#x8BA4;&#x7684;&#x7EA6;&#x5B9A;&#xFF1A;&#x5982;&#x679C;StatefulWidget&#x7684;&#x72B6;&#x6001;&#x662F;&#x5E0C;&#x671B;&#x66B4;&#x9732;&#x51FA;&#x7684;&#xFF0C;&#x5E94;&#x5F53;&#x5728;StatefulWidget&#x4E2D;&#x63D0;&#x4F9B;&#x4E00;&#x4E2A;<code>of</code>&#x9759;&#x6001;&#x65B9;&#x6CD5;&#x6765;&#x83B7;&#x53D6;&#x5176;State&#x5BF9;&#x8C61;&#xFF0C;&#x5F00;&#x53D1;&#x8005;&#x4FBF;&#x53EF;&#x76F4;&#x63A5;&#x901A;&#x8FC7;&#x8BE5;&#x65B9;&#x6CD5;&#x6765;&#x83B7;&#x53D6;&#xFF1B;&#x5982;&#x679C;State&#x4E0D;&#x5E0C;&#x671B;&#x66B4;&#x9732;&#xFF0C;&#x5219;&#x4E0D;&#x63D0;&#x4F9B;<code>of</code>&#x65B9;&#x6CD5;&#x3002;&#x8FD9;&#x4E2A;&#x7EA6;&#x5B9A;&#x5728;Flutter SDK&#x91CC;&#x968F;&#x5904;&#x53EF;&#x89C1;&#x3002;&#x6240;&#x4EE5;&#xFF0C;&#x4E0A;&#x9762;&#x793A;&#x4F8B;&#x4E2D;&#x7684;<code>Scaffold</code>&#x4E5F;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x4E2A;<code>of</code>&#x65B9;&#x6CD5;&#xFF0C;&#x6211;&#x4EEC;&#x5176;&#x5B9E;&#x662F;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x8C03;&#x7528;&#x5B83;&#x7684;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token comment">//&#x7701;&#x7565;&#x65E0;&#x5173;&#x4EE3;&#x7801;</span>
<span class="token comment">// &#x76F4;&#x63A5;&#x901A;&#x8FC7;of&#x9759;&#x6001;&#x65B9;&#x6CD5;&#x6765;&#x83B7;&#x53D6;ScaffoldState </span>
ScaffoldState _state<span class="token operator">=</span>Scaffold<span class="token punctuation">.</span><span class="token function">of</span><span class="token punctuation">(</span>context<span class="token punctuation">)</span><span class="token punctuation">;</span> 
_state<span class="token punctuation">.</span><span class="token function">showSnackBar</span><span class="token punctuation">(</span>
  <span class="token function">SnackBar</span><span class="token punctuation">(</span>
    content<span class="token punctuation">:</span> <span class="token function">Text</span><span class="token punctuation">(</span><span class="token string">&quot;&#x6211;&#x662F;SnackBar&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  <span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<h3 id="&#x901A;&#x8FC7;globalkey">&#x901A;&#x8FC7;GlobalKey</h3>
<p>Flutter&#x8FD8;&#x6709;&#x4E00;&#x79CD;&#x901A;&#x7528;&#x7684;&#x83B7;&#x53D6;<code>State</code>&#x5BF9;&#x8C61;&#x7684;&#x65B9;&#x6CD5;&#x2014;&#x2014;&#x901A;&#x8FC7;GlobalKey&#x6765;&#x83B7;&#x53D6;&#xFF01; &#x6B65;&#x9AA4;&#x5206;&#x4E24;&#x6B65;&#xFF1A;</p>
<ol>
<li><p>&#x7ED9;&#x76EE;&#x6807;<code>StatefulWidget</code>&#x6DFB;&#x52A0;<code>GlobalKey</code>&#x3002;</p>
<pre class="language-"><code class="lang-dart"><span class="token comment">//&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;globalKey, &#x7531;&#x4E8E;GlobalKey&#x8981;&#x4FDD;&#x6301;&#x5168;&#x5C40;&#x552F;&#x4E00;&#x6027;&#xFF0C;&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x9759;&#x6001;&#x53D8;&#x91CF;&#x5B58;&#x50A8;</span>
<span class="token keyword">static</span> GlobalKey<span class="token operator">&lt;</span>ScaffoldState<span class="token operator">&gt;</span> _globalKey<span class="token operator">=</span> <span class="token function">GlobalKey</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token function">Scaffold</span><span class="token punctuation">(</span>
    key<span class="token punctuation">:</span> _globalKey <span class="token punctuation">,</span> <span class="token comment">//&#x8BBE;&#x7F6E;key</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>  
<span class="token punctuation">)</span>
</code></pre>
</li>
<li><p>&#x901A;&#x8FC7;<code>GlobalKey</code>&#x6765;&#x83B7;&#x53D6;<code>State</code>&#x5BF9;&#x8C61;</p>
<pre class="language-"><code class="lang-dart">_globalKey<span class="token punctuation">.</span>currentState<span class="token punctuation">.</span><span class="token function">openDrawer</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre>
</li>
</ol>
<p>GlobalKey&#x662F;Flutter&#x63D0;&#x4F9B;&#x7684;&#x4E00;&#x79CD;&#x5728;&#x6574;&#x4E2A;APP&#x4E2D;&#x5F15;&#x7528;element&#x7684;&#x673A;&#x5236;&#x3002;&#x5982;&#x679C;&#x4E00;&#x4E2A;widget&#x8BBE;&#x7F6E;&#x4E86;<code>GlobalKey</code>&#xFF0C;&#x90A3;&#x4E48;&#x6211;&#x4EEC;&#x4FBF;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;<code>globalKey.currentWidget</code>&#x83B7;&#x5F97;&#x8BE5;widget&#x5BF9;&#x8C61;&#x3001;<code>globalKey.currentElement</code>&#x6765;&#x83B7;&#x5F97;widget&#x5BF9;&#x5E94;&#x7684;element&#x5BF9;&#x8C61;&#xFF0C;&#x5982;&#x679C;&#x5F53;&#x524D;widget&#x662F;<code>StatefulWidget</code>&#xFF0C;&#x5219;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;<code>globalKey.currentState</code>&#x6765;&#x83B7;&#x5F97;&#x8BE5;widget&#x5BF9;&#x5E94;&#x7684;state&#x5BF9;&#x8C61;&#x3002;</p>
<blockquote>
<p>&#x6CE8;&#x610F;&#xFF1A;&#x4F7F;&#x7528;GlobalKey&#x5F00;&#x9500;&#x8F83;&#x5927;&#xFF0C;&#x5982;&#x679C;&#x6709;&#x5176;&#x4ED6;&#x53EF;&#x9009;&#x65B9;&#x6848;&#xFF0C;&#x5E94;&#x5C3D;&#x91CF;&#x907F;&#x514D;&#x4F7F;&#x7528;&#x5B83;&#x3002;&#x53E6;&#x5916;&#x540C;&#x4E00;&#x4E2A;GlobalKey&#x5728;&#x6574;&#x4E2A;widget&#x6811;&#x4E2D;&#x5FC5;&#x987B;&#x662F;&#x552F;&#x4E00;&#x7684;&#xFF0C;&#x4E0D;&#x80FD;&#x91CD;&#x590D;&#x3002;</p>
</blockquote>
<h2 id="318-flutter-sdk&#x5185;&#x7F6E;&#x7EC4;&#x4EF6;&#x5E93;&#x4ECB;&#x7ECD;">3.1.8 Flutter SDK&#x5185;&#x7F6E;&#x7EC4;&#x4EF6;&#x5E93;&#x4ECB;&#x7ECD;</h2>
<p>Flutter&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x5957;&#x4E30;&#x5BCC;&#x3001;&#x5F3A;&#x5927;&#x7684;&#x57FA;&#x7840;&#x7EC4;&#x4EF6;&#xFF0C;&#x5728;&#x57FA;&#x7840;&#x7EC4;&#x4EF6;&#x5E93;&#x4E4B;&#x4E0A;Flutter&#x53C8;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x5957;Material&#x98CE;&#x683C;&#xFF08;Android&#x9ED8;&#x8BA4;&#x7684;&#x89C6;&#x89C9;&#x98CE;&#x683C;&#xFF09;&#x548C;&#x4E00;&#x5957;Cupertino&#x98CE;&#x683C;&#xFF08;iOS&#x89C6;&#x89C9;&#x98CE;&#x683C;&#xFF09;&#x7684;&#x7EC4;&#x4EF6;&#x5E93;&#x3002;&#x8981;&#x4F7F;&#x7528;&#x57FA;&#x7840;&#x7EC4;&#x4EF6;&#x5E93;&#xFF0C;&#x9700;&#x8981;&#x5148;&#x5BFC;&#x5165;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"><span class="token keyword">import</span> <span class="token string">&apos;package:flutter/widgets.dart&apos;</span><span class="token punctuation">;</span>
</code></pre>
<p>&#x4E0B;&#x9762;&#x6211;&#x4EEC;&#x4ECB;&#x7ECD;&#x4E00;&#x4E0B;&#x5E38;&#x7528;&#x7684;&#x7EC4;&#x4EF6;&#x3002;</p>
<h4 id="&#x57FA;&#x7840;&#x7EC4;&#x4EF6;">&#x57FA;&#x7840;&#x7EC4;&#x4EF6;</h4>
<ul>
<li><a href="https://docs.flutter.io/flutter/widgets/Text-class.html" target="_blank"><code>Text</code></a>&#xFF1A;&#x8BE5;&#x7EC4;&#x4EF6;&#x53EF;&#x8BA9;&#x60A8;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5E26;&#x683C;&#x5F0F;&#x7684;&#x6587;&#x672C;&#x3002;</li>
<li><a href="https://docs.flutter.io/flutter/widgets/Row-class.html" target="_blank"><code>Row</code></a>&#x3001; <a href="https://docs.flutter.io/flutter/widgets/Column-class.html" target="_blank"><code>Column</code></a>&#xFF1A; &#x8FD9;&#x4E9B;&#x5177;&#x6709;&#x5F39;&#x6027;&#x7A7A;&#x95F4;&#x7684;&#x5E03;&#x5C40;&#x7C7B;Widget&#x53EF;&#x8BA9;&#x60A8;&#x5728;&#x6C34;&#x5E73;&#xFF08;Row&#xFF09;&#x548C;&#x5782;&#x76F4;&#xFF08;Column&#xFF09;&#x65B9;&#x5411;&#x4E0A;&#x521B;&#x5EFA;&#x7075;&#x6D3B;&#x7684;&#x5E03;&#x5C40;&#x3002;&#x5176;&#x8BBE;&#x8BA1;&#x662F;&#x57FA;&#x4E8E;Web&#x5F00;&#x53D1;&#x4E2D;&#x7684;Flexbox&#x5E03;&#x5C40;&#x6A21;&#x578B;&#x3002;</li>
<li><a href="https://docs.flutter.io/flutter/widgets/Stack-class.html" target="_blank"><code>Stack</code></a>&#xFF1A; &#x53D6;&#x4EE3;&#x7EBF;&#x6027;&#x5E03;&#x5C40; (&#x8BD1;&#x8005;&#x8BED;&#xFF1A;&#x548C;Android&#x4E2D;&#x7684;<code>FrameLayout</code>&#x76F8;&#x4F3C;)&#xFF0C;<a href="https://docs.flutter.io/flutter/widgets/Stack-class.html" target="_blank"><code>Stack</code></a>&#x5141;&#x8BB8;&#x5B50; widget &#x5806;&#x53E0;&#xFF0C; &#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <a href="https://docs.flutter.io/flutter/widgets/Positioned-class.html" target="_blank"><code>Positioned</code></a> &#x6765;&#x5B9A;&#x4F4D;&#x4ED6;&#x4EEC;&#x76F8;&#x5BF9;&#x4E8E;<code>Stack</code>&#x7684;&#x4E0A;&#x4E0B;&#x5DE6;&#x53F3;&#x56DB;&#x6761;&#x8FB9;&#x7684;&#x4F4D;&#x7F6E;&#x3002;Stacks&#x662F;&#x57FA;&#x4E8E;Web&#x5F00;&#x53D1;&#x4E2D;&#x7684;&#x7EDD;&#x5BF9;&#x5B9A;&#x4F4D;&#xFF08;absolute positioning )&#x5E03;&#x5C40;&#x6A21;&#x578B;&#x8BBE;&#x8BA1;&#x7684;&#x3002;</li>
<li><a href="https://docs.flutter.io/flutter/widgets/Container-class.html" target="_blank"><code>Container</code></a>&#xFF1A; <a href="https://docs.flutter.io/flutter/widgets/Container-class.html" target="_blank"><code>Container</code></a> &#x53EF;&#x8BA9;&#x60A8;&#x521B;&#x5EFA;&#x77E9;&#x5F62;&#x89C6;&#x89C9;&#x5143;&#x7D20;&#x3002;container &#x53EF;&#x4EE5;&#x88C5;&#x9970;&#x4E00;&#x4E2A;<a href="https://docs.flutter.io/flutter/painting/BoxDecoration-class.html" target="_blank"><code>BoxDecoration</code></a>, &#x5982; background&#x3001;&#x4E00;&#x4E2A;&#x8FB9;&#x6846;&#x3001;&#x6216;&#x8005;&#x4E00;&#x4E2A;&#x9634;&#x5F71;&#x3002; <a href="https://docs.flutter.io/flutter/widgets/Container-class.html" target="_blank"><code>Container</code></a> &#x4E5F;&#x53EF;&#x4EE5;&#x5177;&#x6709;&#x8FB9;&#x8DDD;&#xFF08;margins&#xFF09;&#x3001;&#x586B;&#x5145;(padding)&#x548C;&#x5E94;&#x7528;&#x4E8E;&#x5176;&#x5927;&#x5C0F;&#x7684;&#x7EA6;&#x675F;(constraints)&#x3002;&#x53E6;&#x5916;&#xFF0C; <a href="https://docs.flutter.io/flutter/widgets/Container-class.html" target="_blank"><code>Container</code></a>&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x77E9;&#x9635;&#x5728;&#x4E09;&#x7EF4;&#x7A7A;&#x95F4;&#x4E2D;&#x5BF9;&#x5176;&#x8FDB;&#x884C;&#x53D8;&#x6362;&#x3002;</li>
</ul>
<h4 id="material&#x7EC4;&#x4EF6;">Material&#x7EC4;&#x4EF6;</h4>
<p>Flutter&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x5957;&#x4E30;&#x5BCC;&#x7684;Material&#x7EC4;&#x4EF6;&#xFF0C;&#x5B83;&#x53EF;&#x4EE5;&#x5E2E;&#x52A9;&#x6211;&#x4EEC;&#x6784;&#x5EFA;&#x9075;&#x5FAA;Material Design&#x8BBE;&#x8BA1;&#x89C4;&#x8303;&#x7684;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x3002;Material&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x4EE5;<a href="https://docs.flutter.io/flutter/material/MaterialApp-class.html" target="_blank"><code>MaterialApp</code></a> &#x7EC4;&#x4EF6;&#x5F00;&#x59CB;&#xFF0C; &#x8BE5;&#x7EC4;&#x4EF6;&#x5728;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x7684;&#x6839;&#x90E8;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E9B;&#x5FC5;&#x8981;&#x7684;&#x7EC4;&#x4EF6;&#xFF0C;&#x6BD4;&#x5982;<code>Theme</code>&#x7EC4;&#x4EF6;&#xFF0C;&#x5B83;&#x7528;&#x4E8E;&#x914D;&#x7F6E;&#x5E94;&#x7528;&#x7684;&#x4E3B;&#x9898;&#x3002; &#x662F;&#x5426;&#x4F7F;&#x7528;<a href="https://docs.flutter.io/flutter/material/MaterialApp-class.html" target="_blank"><code>MaterialApp</code></a>&#x5B8C;&#x5168;&#x662F;&#x53EF;&#x9009;&#x7684;&#xFF0C;&#x4F46;&#x662F;&#x4F7F;&#x7528;&#x5B83;&#x662F;&#x4E00;&#x4E2A;&#x5F88;&#x597D;&#x7684;&#x505A;&#x6CD5;&#x3002;&#x5728;&#x4E4B;&#x524D;&#x7684;&#x793A;&#x4F8B;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x4F7F;&#x7528;&#x8FC7;&#x591A;&#x4E2A;Material &#x7EC4;&#x4EF6;&#x4E86;&#xFF0C;&#x5982;&#xFF1A;<code>Scaffold</code>&#x3001;<code>AppBar</code>&#x3001;<code>FlatButton</code>&#x7B49;&#x3002;&#x8981;&#x4F7F;&#x7528;Material &#x7EC4;&#x4EF6;&#xFF0C;&#x9700;&#x8981;&#x5148;&#x5F15;&#x5165;&#x5B83;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"><span class="token keyword">import</span> <span class="token string">&apos;package:flutter/material.dart&apos;</span><span class="token punctuation">;</span>
</code></pre>
<h4 id="cupertino&#x7EC4;&#x4EF6;">Cupertino&#x7EC4;&#x4EF6;</h4>
<p>Flutter&#x4E5F;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x5957;&#x4E30;&#x5BCC;&#x7684;Cupertino&#x98CE;&#x683C;&#x7684;&#x7EC4;&#x4EF6;&#xFF0C;&#x5C3D;&#x7BA1;&#x76EE;&#x524D;&#x8FD8;&#x6CA1;&#x6709;Material &#x7EC4;&#x4EF6;&#x90A3;&#x4E48;&#x4E30;&#x5BCC;&#xFF0C;&#x4F46;&#x662F;&#x5B83;&#x4ECD;&#x5728;&#x4E0D;&#x65AD;&#x7684;&#x5B8C;&#x5584;&#x4E2D;&#x3002;&#x503C;&#x5F97;&#x4E00;&#x63D0;&#x7684;&#x662F;&#x5728;Material &#x7EC4;&#x4EF6;&#x5E93;&#x4E2D;&#x6709;&#x4E00;&#x4E9B;&#x7EC4;&#x4EF6;&#x53EF;&#x4EE5;&#x6839;&#x636E;&#x5B9E;&#x9645;&#x8FD0;&#x884C;&#x5E73;&#x53F0;&#x6765;&#x5207;&#x6362;&#x8868;&#x73B0;&#x98CE;&#x683C;&#xFF0C;&#x6BD4;&#x5982;<code>MaterialPageRoute</code>&#xFF0C;&#x5728;&#x8DEF;&#x7531;&#x5207;&#x6362;&#x65F6;&#xFF0C;&#x5982;&#x679C;&#x662F;Android&#x7CFB;&#x7EDF;&#xFF0C;&#x5B83;&#x5C06;&#x4F1A;&#x4F7F;&#x7528;Android&#x7CFB;&#x7EDF;&#x9ED8;&#x8BA4;&#x7684;&#x9875;&#x9762;&#x5207;&#x6362;&#x52A8;&#x753B;(&#x4ECE;&#x5E95;&#x5411;&#x4E0A;)&#xFF1B;&#x5982;&#x679C;&#x662F;iOS&#x7CFB;&#x7EDF;&#xFF0C;&#x5B83;&#x4F1A;&#x4F7F;&#x7528;iOS&#x7CFB;&#x7EDF;&#x9ED8;&#x8BA4;&#x7684;&#x9875;&#x9762;&#x5207;&#x6362;&#x52A8;&#x753B;&#xFF08;&#x4ECE;&#x53F3;&#x5411;&#x5DE6;&#xFF09;&#x3002;&#x7531;&#x4E8E;&#x5728;&#x524D;&#x9762;&#x7684;&#x793A;&#x4F8B;&#x4E2D;&#x8FD8;&#x6CA1;&#x6709;Cupertino&#x7EC4;&#x4EF6;&#x7684;&#x793A;&#x4F8B;&#xFF0C;&#x4E0B;&#x9762;&#x6211;&#x4EEC;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;Cupertino&#x7EC4;&#x4EF6;&#x98CE;&#x683C;&#x7684;&#x9875;&#x9762;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"><span class="token comment">//&#x5BFC;&#x5165;cupertino widget&#x5E93;</span>
<span class="token keyword">import</span> <span class="token string">&apos;package:flutter/cupertino.dart&apos;</span><span class="token punctuation">;</span>

<span class="token keyword">class</span> <span class="token class-name">CupertinoTestRoute</span> <span class="token keyword">extends</span> <span class="token class-name">StatelessWidget</span> <span class="token punctuation">{</span>
  <span class="token metadata symbol">@override</span>
  Widget <span class="token function">build</span><span class="token punctuation">(</span>BuildContext context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token function">CupertinoPageScaffold</span><span class="token punctuation">(</span>
      navigationBar<span class="token punctuation">:</span> <span class="token function">CupertinoNavigationBar</span><span class="token punctuation">(</span>
        middle<span class="token punctuation">:</span> <span class="token function">Text</span><span class="token punctuation">(</span><span class="token string">&quot;Cupertino Demo&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
      <span class="token punctuation">)</span><span class="token punctuation">,</span>
      child<span class="token punctuation">:</span> <span class="token function">Center</span><span class="token punctuation">(</span>
        child<span class="token punctuation">:</span> <span class="token function">CupertinoButton</span><span class="token punctuation">(</span>
            color<span class="token punctuation">:</span> CupertinoColors<span class="token punctuation">.</span>activeBlue<span class="token punctuation">,</span>
            child<span class="token punctuation">:</span> <span class="token function">Text</span><span class="token punctuation">(</span><span class="token string">&quot;Press&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            onPressed<span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
        <span class="token punctuation">)</span><span class="token punctuation">,</span>
      <span class="token punctuation">)</span><span class="token punctuation">,</span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x4E0B;&#x9762;&#xFF08;&#x56FE;3-3&#xFF09;&#x662F;&#x5728;iPhoneX&#x4E0A;&#x9875;&#x9762;&#x6548;&#x679C;&#x622A;&#x56FE;&#xFF1A;</p>
<p><img src="https://pcdn.flutterchina.club/imgs/3-3.png" alt="&#x56FE;3-3"></p>
<h3 id="&#x5173;&#x4E8E;&#x793A;&#x4F8B;">&#x5173;&#x4E8E;&#x793A;&#x4F8B;</h3>
<p>&#x672C;&#x7AE0;&#x540E;&#x9762;&#x7AE0;&#x8282;&#x7684;&#x793A;&#x4F8B;&#x4E2D;&#x4F1A;&#x4F7F;&#x7528;&#x4E00;&#x4E9B;&#x5E03;&#x5C40;&#x7C7B;&#x7EC4;&#x4EF6;&#xFF0C;&#x5982;<code>Scaffold</code>&#x3001;<code>Row</code>&#x3001;<code>Column</code>&#x7B49;&#xFF0C;&#x8FD9;&#x4E9B;&#x7EC4;&#x4EF6;&#x5C06;&#x5728;&#x540E;&#x9762;&#x201C;&#x5E03;&#x5C40;&#x7C7B;&#x7EC4;&#x4EF6;&#x201D;&#x4E00;&#x7AE0;&#x4E2D;&#x8BE6;&#x7EC6;&#x4ECB;&#x7ECD;&#xFF0C;&#x8BFB;&#x8005;&#x53EF;&#x4EE5;&#x5148;&#x4E0D;&#x7528;&#x5173;&#x6CE8;&#x3002;</p>
<h3 id="&#x603B;&#x7ED3;">&#x603B;&#x7ED3;</h3>
<p>Flutter&#x63D0;&#x4F9B;&#x4E86;&#x4E30;&#x5BCC;&#x7684;&#x7EC4;&#x4EF6;&#xFF0C;&#x5728;&#x5B9E;&#x9645;&#x7684;&#x5F00;&#x53D1;&#x4E2D;&#x4F60;&#x53EF;&#x4EE5;&#x6839;&#x636E;&#x9700;&#x8981;&#x968F;&#x610F;&#x4F7F;&#x7528;&#x5B83;&#x4EEC;&#xFF0C;&#x800C;&#x4E0D;&#x5FC5;&#x62C5;&#x5FC3;&#x5F15;&#x5165;&#x8FC7;&#x591A;&#x7EC4;&#x4EF6;&#x5E93;&#x4F1A;&#x8BA9;&#x4F60;&#x7684;&#x5E94;&#x7528;&#x5B89;&#x88C5;&#x5305;&#x53D8;&#x5927;&#xFF0C;&#x8FD9;&#x4E0D;&#x662F;web&#x5F00;&#x53D1;&#xFF0C;dart&#x5728;&#x7F16;&#x8BD1;&#x65F6;&#x53EA;&#x4F1A;&#x7F16;&#x8BD1;&#x4F60;&#x4F7F;&#x7528;&#x4E86;&#x7684;&#x4EE3;&#x7801;&#x3002;&#x7531;&#x4E8E;Material&#x548C;Cupertino&#x90FD;&#x662F;&#x5728;&#x57FA;&#x7840;&#x7EC4;&#x4EF6;&#x5E93;&#x4E4B;&#x4E0A;&#x7684;&#xFF0C;&#x6240;&#x4EE5;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x7684;&#x5E94;&#x7528;&#x4E2D;&#x5F15;&#x5165;&#x4E86;&#x8FD9;&#x4E24;&#x8005;&#x4E4B;&#x4E00;&#xFF0C;&#x5219;&#x4E0D;&#x9700;&#x8981;&#x518D;&#x5F15;&#x5165;<code>flutter/widgets.dart</code>&#x4E86;&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x4EEC;&#x5185;&#x90E8;&#x5DF2;&#x7ECF;&#x5F15;&#x5165;&#x8FC7;&#x4E86;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="./" class="navigation navigation-prev " aria-label="Previous page: 第三章：基础组件">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="state_manage.html" class="navigation navigation-next " aria-label="Next page: 3.2：状态管理">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"3.1：Widget简介","level":"2.3.1","depth":2,"next":{"title":"3.2：状态管理","level":"2.3.2","depth":2,"path":"chapter3/state_manage.md","ref":"chapter3/state_manage.md","articles":[]},"previous":{"title":"第三章：基础组件","level":"2.3","depth":1,"path":"chapter3/index.md","ref":"chapter3/index.md","articles":[{"title":"3.1：Widget简介","level":"2.3.1","depth":2,"path":"chapter3/flutter_widget_intro.md","ref":"chapter3/flutter_widget_intro.md","articles":[]},{"title":"3.2：状态管理","level":"2.3.2","depth":2,"path":"chapter3/state_manage.md","ref":"chapter3/state_manage.md","articles":[]},{"title":"3.3：文本、字体样式","level":"2.3.3","depth":2,"path":"chapter3/text.md","ref":"chapter3/text.md","articles":[]},{"title":"3.4：按钮","level":"2.3.4","depth":2,"path":"chapter3/buttons.md","ref":"chapter3/buttons.md","articles":[]},{"title":"3.5：图片和Icon","level":"2.3.5","depth":2,"path":"chapter3/img_and_icon.md","ref":"chapter3/img_and_icon.md","articles":[]},{"title":"3.6：单选框和复选框","level":"2.3.6","depth":2,"path":"chapter3/radio_and_checkbox.md","ref":"chapter3/radio_and_checkbox.md","articles":[]},{"title":"3.7：输入框和表单","level":"2.3.7","depth":2,"path":"chapter3/input_and_form.md","ref":"chapter3/input_and_form.md","articles":[]},{"title":"3.8：进度指示器","level":"2.3.8","depth":2,"path":"chapter3/progress.md","ref":"chapter3/progress.md","articles":[]}]},"dir":"ltr"},"config":{"plugins":["-search","search-pro","ace","splitter","prism","donate","-highlight"],"root":"docs","styles":{"website":"styles/website.css","ebook":"styles/ebook.css","pdf":"styles/pdf.css","mobi":"styles/mobi.css","epub":"styles/epub.css"},"pluginsConfig":{"prism":{"lang":{"shell":"bash"}},"ace":{},"splitter":{},"search-pro":{"cutWordLib":"nodejieba","defineWord":["Gitbook Use"]},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"donate":{"alipay":"","alipayText":"支付宝打赏","button":"赏","title":"","wechat":"https://cdn.jsdelivr.net/gh/flutterchina/flutter-in-action@1.0.3/docs/imgs/pay.jpeg","wechatText":"微信打赏"},"fontsettings":{"theme":"white","family":"sans","size":2},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"wendux 824783146@qq.com","bookRoot":"https://book.flutterchina.club/","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"《Flutter实战》","language":"zh-hans","links":{"sharing":{"all":null,"facebook":null,"google":null,"twitter":null,"weibo":null},"sidebar":{"<div><img height=20 style='position:relative; top:5px; padding-right:5px ' src='https://cdn.jsdelivr.net/gh/flutterchina/website@1.0/images/favicon.png'/> Flutter中文网</div>":"https://flutterchina.club"}},"gitbook":"*","description":"记录Gitbook的配置和一些插件的使用"},"file":{"path":"chapter3/flutter_widget_intro.md","mtime":"2019-11-25T08:35:49.894Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-03-15T14:52:22.710Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>



    <script src="https://pcdn.flutterchina.club/gitbook/gitbook.js"></script>
    <script src="https://pcdn.flutterchina.club/gitbook/theme.js"></script>
    
        
        <script src="https://pcdn.flutterchina.club/gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="https://pcdn.flutterchina.club/gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="https://pcdn.flutterchina.club/gitbook/gitbook-plugin-ace/ace/ace.js"></script>
        
    
        
        <script src="https://pcdn.flutterchina.club/gitbook/gitbook-plugin-ace/ace.js"></script>
        
    
        
        <script src="https://pcdn.flutterchina.club/gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="https://pcdn.flutterchina.club/gitbook/gitbook-plugin-donate/plugin.js"></script>
        
    
        
        <script src="https://pcdn.flutterchina.club/gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="https://pcdn.flutterchina.club/gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="https://pcdn.flutterchina.club/gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="https://pcdn.flutterchina.club/gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    


<script src="https://pcdn.flutterchina.club/gitbook/pub.js"></script>
<script>
    hookAjax({
        open: function (arg, xhr) {
            if (location.hostname !== 'localhost') {
                if (arg[1][0] === '.') arg[1] = arg[1].slice(1);
                arg[1] = "https://pcdn.flutterchina.club" + arg[1].replace(".html", ".1")
            }
        },
        setRequestHeader: function (arg, xhr) {
            console.log(arg[0], arg[1]);
            if (arg[0] !== 'Accept') return true;
        },
        onload:function(){
            setTimeout(addAD);
        }
    })
</script>
</body>

</html>

